Surrounded Regions leetcode java
题目:
Given a 2D board containing 'X'
and 'O'
, capture all regions surrounded by 'X'
.
A region is captured by flipping all 'O'
s into 'X'
s in that surrounded region.
For example,
X X X X X O O X X X O X X O X X
After running your function, the board should be:
X X X X X X X X X X X X X O X X
题解:
这道题是说一个O周围都是X那么这个O就得变成X。那么就可以发现四周这一圈如果有O肯定不能四周都被X包围,同时这个O也将会是潜在的内部的O的缺口,
让内部的O不能都被X覆盖。因此,思路就是先对四周的O进行特殊处理,用BFS走,把所有这个O连接的O(包括这个O)都涂成#。这样子,对于原来的棋盘来说,
没有变成#的O就是四周没有被O污染的,四周都是X,那么对其变成X。而所有#就是那些原来是O但是不是四周都被X包围的,把它们再还原成O。
代码如下:
1 public void solve(char[][] board) {
2 if(board==null || board.length<=1 || board[0].length<=1)
3 return;
4
5 //第一行和最后一行进行fill
6 for(int i=0;i<board[0].length;i++){
7 fill(board,0,i);
8 fill(board,board.length-1,i);
9 }
10
11 //第一列和最后一列进行fill
12 for(int i=0;i<board.length;i++){
13 fill(board,i,0);
14 fill(board,i,board[0].length-1);
15 }
16
17 //对于当前格子中,所有O变成X(说明符合要求,没有被变成#),所有#变成O
18 for(int i=0;i<board.length;i++){
19 for(int j=0;j<board[0].length;j++){
20 if(board[i][j]=='O')
21 board[i][j]='X';
22 else if(board[i][j]=='#')
23 board[i][j]='O';
24 }
25 }
26 }
27
28 private void fill(char[][] board, int i, int j){
29 if(board[i][j]!='O')
30 return;
31 board[i][j] = '#';
32 //利用BFS
33 LinkedList<Integer> queue = new LinkedList<Integer>();
34 //先将矩阵的横纵坐标编码
35 int code = i*board[0].length+j;
36 queue.add(code);
37 while(!queue.isEmpty()){
38 code = queue.poll();
39 int row = code/board[0].length;//从code中还原横坐标
40 int col = code%board[0].length;//从code中还原纵坐标
41
42 if(row>=1 && board[row-1][col]=='O'){//当前元素上边是否为0
43 queue.add((row-1)*board[0].length + col);
44 board[row-1][col]='#';
45 }
46
47 if(row<=board.length-2 && board[row+1][col]=='O'){//当前元素下面是否为0
48 queue.add((row+1)*board[0].length + col);
49 board[row+1][col]='#';
50 }
51
52 if(col>=1 && board[row][col-1]=='O'){//当前元素左边是否为0
53 queue.add(row*board[0].length + col-1);
54 board[row][col-1]='#';
55 }
56
57 if(col<=board[0].length-2 && board[row][col+1]=='O'){//当前元素右边是否为0
58 queue.add(row*board[0].length + col+1);
59 board[row][col+1]='#';
60 }
61 }
62 }
2 if(board==null || board.length<=1 || board[0].length<=1)
3 return;
4
5 //第一行和最后一行进行fill
6 for(int i=0;i<board[0].length;i++){
7 fill(board,0,i);
8 fill(board,board.length-1,i);
9 }
10
11 //第一列和最后一列进行fill
12 for(int i=0;i<board.length;i++){
13 fill(board,i,0);
14 fill(board,i,board[0].length-1);
15 }
16
17 //对于当前格子中,所有O变成X(说明符合要求,没有被变成#),所有#变成O
18 for(int i=0;i<board.length;i++){
19 for(int j=0;j<board[0].length;j++){
20 if(board[i][j]=='O')
21 board[i][j]='X';
22 else if(board[i][j]=='#')
23 board[i][j]='O';
24 }
25 }
26 }
27
28 private void fill(char[][] board, int i, int j){
29 if(board[i][j]!='O')
30 return;
31 board[i][j] = '#';
32 //利用BFS
33 LinkedList<Integer> queue = new LinkedList<Integer>();
34 //先将矩阵的横纵坐标编码
35 int code = i*board[0].length+j;
36 queue.add(code);
37 while(!queue.isEmpty()){
38 code = queue.poll();
39 int row = code/board[0].length;//从code中还原横坐标
40 int col = code%board[0].length;//从code中还原纵坐标
41
42 if(row>=1 && board[row-1][col]=='O'){//当前元素上边是否为0
43 queue.add((row-1)*board[0].length + col);
44 board[row-1][col]='#';
45 }
46
47 if(row<=board.length-2 && board[row+1][col]=='O'){//当前元素下面是否为0
48 queue.add((row+1)*board[0].length + col);
49 board[row+1][col]='#';
50 }
51
52 if(col>=1 && board[row][col-1]=='O'){//当前元素左边是否为0
53 queue.add(row*board[0].length + col-1);
54 board[row][col-1]='#';
55 }
56
57 if(col<=board[0].length-2 && board[row][col+1]=='O'){//当前元素右边是否为0
58 queue.add(row*board[0].length + col+1);
59 board[row][col+1]='#';
60 }
61 }
62 }
Reference:http://blog.csdn.net/linhuanmars/article/details/22904855