forked from xiaoyaoworm/Leetcode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
130_surroundedRegions.java
86 lines (74 loc) · 2.51 KB
/
130_surroundedRegions.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
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
76
77
78
79
80
81
82
83
84
85
86
用DFS去递归解,
public class Solution {
public void solve(char[][] board) {
if(board == null || board.length == 0) return;
int row = board.length;
int col = board[0].length;
for(int i = 0; i < row; i ++){
for(int j = 0; j < col; j++){
if(i == 0 || i == row-1 || j == 0 || j == col-1){
if(board[i][j] == 'O'){
dfs(board,i,j);
}
}
}
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(board[i][j] == 'Z') board[i][j] = 'O';
else board[i][j] = 'X';
}
}
}
public void dfs(char[][] board, int x, int y){
int row = board.length;
int col = board[0].length;
board[x][y] = 'Z';
if(x-1>0 && board[x-1][y] == 'O') dfs(board,x-1,y);
if(x+1<row-1 && board[x+1][y] == 'O') dfs(board,x+1,y);
if(y-1>0 && board[x][y-1] == 'O') dfs(board,x,y-1);
if(y+1<col-1 && board[x][y+1]=='O') dfs(board,x,y+1);
}
}
用BFS去解:
public class Solution {
public class Pair{
int x;
int y;
public Pair(int x, int y){
this.x = x;
this.y = y;
}
}
public void solve(char[][] board) {
if(board == null || board.length == 0) return;
int row = board.length;
int col = board[0].length;
Queue<Pair> queue = new LinkedList<Pair>();
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(i == 0 || i == row-1 || j == 0 || j == col-1){
if(board[i][j] =='O'){
queue.add(new Pair(i,j));
}
}
}
}
while(!queue.isEmpty()){
Pair temp = queue.remove();
int x = temp.x;
int y = temp.y;
board[x][y] = 'Z';
if(x-1>0 && board[x-1][y] == 'O') queue.add(new Pair(x-1,y));
if(x+1<row-1 && board[x+1][y] == 'O') queue.add(new Pair(x+1,y));
if(y-1>0 && board[x][y-1] == 'O') queue.add(new Pair(x,y-1));
if(y+1<col-1 && board[x][y+1] == 'O') queue.add(new Pair(x,y+1));
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++){
if(board[i][j]=='Z') board[i][j] = 'O';
else board[i][j] = 'X';
}
}
}
}