-
Notifications
You must be signed in to change notification settings - Fork 0
/
15Jan2023.java
39 lines (34 loc) · 1.25 KB
/
15Jan2023.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int water_flow(int [][] mat, int N, int M) {
//Write your code here
boolean indian[][]=new boolean[N][M];
boolean arabian[][]=new boolean[N][M];
for(int i=0;i<N;i++) dfs(mat, indian, i, 0, N, M);
for(int i=0;i<M;i++) dfs(mat, indian, 0, i, N, M);
for(int i=0;i<N;i++) dfs(mat, arabian, i, M-1, N, M);
for(int i=0;i<M;i++) dfs(mat, arabian, N-1, i, N, M);
int count = 0;
for(int i=0;i<N;i++){
for(int j=0;j<M;j++){
if(indian[i][j] && arabian[i][j]){
count++;
}
}
}
return count;
}
void dfs(int mat[][], boolean vis[][], int i, int j, int n, int m){
if(i<0 || j<0 || i>=n || j>=m || vis[i][j]) return;
vis[i][j] = true;
if(i>0 && !vis[i-1][j] && mat[i-1][j] >= mat[i][j]){
dfs(mat, vis, i-1, j, n, m);
}
if(i<n-1 && !vis[i+1][j] && mat[i+1][j] >= mat[i][j]){
dfs(mat, vis, i+1, j, n, m);
}
if(j>0 && !vis[i][j-1] && mat[i][j-1] >= mat[i][j]){
dfs(mat, vis, i, j-1, n, m);
}
if(j<m-1 && !vis[i][j+1] && mat[i][j+1] >= mat[i][j]){
dfs(mat, vis, i, j+1, n, m);
}
}