forked from SR-Sunny-Raj/Hacktoberfest2021-DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWallsAndGatesBFS.java
50 lines (48 loc) · 1.04 KB
/
WallsAndGatesBFS.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
40
41
42
43
44
45
46
47
48
49
50
package Graphs;
import java.util.LinkedList;
import java.util.Queue;
public class WallsAndGatesBFS {
class Pair{
int x;
int y;
Pair(int x, int y){
this.x = x;
this.y = y;
}
}
public int[][] solve(int[][] matrix){
Queue<Pair> q = new LinkedList<>();
boolean[][] visited = new boolean[matrix.length][matrix[0].length];
int[][] dir = {{0,1},{1,0},{-1,0},{0,-1}};
for(int i=0;i<matrix.length;i++){
for(int j=0;j<matrix[0].length;j++){
if(matrix[i][j]==0){
q.add(new Pair(i, j));
visited[i][j]=true;
}
}
}
int level = 0;
while(!q.isEmpty()){
level++;
int size = q.size();
while(size>0){
size--;
Pair p = q.poll();
for(int i=0;i<dir.length;i++){
int r = p.x+ dir[i][0];
int c = p.y + dir[i][1];
//Validity Check- should not be a wall
if(r>=0 && r<matrix.length&& c>=0 && c<matrix[0].length&& matrix[r][c]!=-1){
if(!visited[r][c]){
matrix[r][c] = level;
q.add(new Pair(r, c));
visited[r][c] = true;
}
}
}
}
}
return matrix;
}
}