題目:
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