-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSurroundedRegions.java
More file actions
75 lines (58 loc) · 1.99 KB
/
SurroundedRegions.java
File metadata and controls
75 lines (58 loc) · 1.99 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
public class SurroundedRegions {
public void solve(char[][] board) {
if(board == null || board.length ==0) return;
int lenX = board.length ;
int lenY = board[0].length;
//Queue<Integer> queue = new LinkedList<Integer> ();
for(int i=0; i<lenX; i++) {
fill(i,0, board);
fill(i, lenY-1, board);
}
for(int i=0; i<lenY; i++) {
fill(0,i, board);
fill(lenX-1, i, board);
}
for(int i=0; i<lenX; i++) {
for(int j=0; j<lenY; j++) {
if(board[i][j] == 'Y') {
board[i][j] = 'O';
} else if(board[i][j] == 'O')
board[i][j] = 'X';
}
}
return;
}
private void fill (int x, int y, char[][] board) {
if(board[x][y] != 'O') return;
board[x][y] = 'Y';
Queue<Integer> qc = new LinkedList<Integer>();
Queue<Integer> qr = new LinkedList<Integer>();
qr.add(x);
qc.add(y);
while(qr.size() != 0) {
int row = qr.poll();
int col = qc.poll();
if(row>0 && board[row-1][col] == 'O') {
board[row-1][col] = 'Y';
qr.add(row-1);
qc.add(col);
}
if(row<board.length-1 && board[row+1][col] == 'O') {
board[row+1][col] = 'Y';
qr.add(row+1);
qc.add(col);
}
if(col>0 && board[row][col-1] == 'O') {
board[row][col-1] = 'Y';
qr.add(row);
qc.add(col-1);
}
if(col<board[0].length-1 && board[row][col+1] == 'O') {
board[row][col+1] = 'Y';
qr.add(row);
qc.add(col+1);
}
}
return;
}
}