-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path130.surrounded regions created
62 lines (59 loc) · 1.35 KB
/
130.surrounded regions created
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
51
52
53
54
55
56
57
58
59
60
61
62
class Solution {
class Pair{
int fi;
int sec;
Pair(int f,int s)
{
this.fi=f;
this.sec=s;
}
}
public void solve(char[][] board) {
int n=board.length;
int m=board[0].length;
for(int l=0;l<n;l++)
{
for(int p=0;p<m;p++){
if(l==0||l==n-1|| p==0|| p==m-1){
if(board[l][p]=='O'){
bfs(board,l,p);
}
}
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(board[i][j]=='O'){
board[i][j]='X';
}
if(board[i][j]=='T'){
board[i][j]='O';
}
}
}
}
void bfs(char[][] board,int i,int j)
{ int n=board.length;
int m=board[0].length;
Queue<Pair> qu=new LinkedList<>();
qu.add(new Pair(i,j));
board[i][j]='T';
while(!qu.isEmpty())
{
Pair top=qu.poll();
int row[]={-1,1,0,0};
int col[]={0,0,-1,1};
for(int k=0;k<4;k++)
{
int nrow=row[k]+top.fi;
int ncol=col[k]+top.sec;
while(nrow>=0 && nrow<=n-1 && ncol>=0 && ncol<=m-1 && board[nrow][ncol]=='O'){
board[nrow][ncol]='T';
qu.add(new Pair(nrow,ncol));
}
}
}
}
}