Given a 2D board containing `'X'` and `'O'` (the letter O), capture all regions surrounded by `'X'`.

A region is captured by flipping all `'O'`s into `'X'`s in that surrounded region.

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
```

Explanation:

Surrounded regions shouldn’t be on the border, which means that any `'O'` on the border of the board are not flipped to `'X'`. Any `'O'` that is not on the border and it is not connected to an `'O'` on the border will be flipped to `'X'`. Two cells are connected if they are adjacent cells connected horizontally or vertically.

Note: 无论是BFS还是DFS都应该有个used来check 之前是否visit 过这个cell. 否则就会无限循环. 这里可以先在board上更改, 再进queue, 如果出queue再更改就会有重复进queue的情况.

Time Complexity: O(m*n).

Space: O(m*n).

AC Java:

``` 1 class Solution {
2     private int [][] dirs = {{-1, 0},{1, 0},{0, -1},{0, 1}};
3
4     public void solve(char[][] board) {
5         if(board == null || board.length == 0 || board[0].length == 0){
6             return;
7         }
8
9         int m = board.length;
10         int n = board[0].length;
11         for(int j = 0; j<n; j++){
12             if(board[0][j] == 'O'){
13                 // bfs(board, 0, j);
14                 dfs(board, 0, j);
15             }
16
17             if(board[m-1][j] == 'O'){
18                 // bfs(board, m-1, j);
19                 dfs(board, m-1, j);
20             }
21         }
22
23         for(int i = 0; i<m; i++){
24             if(board[i][0] == 'O'){
25                 // bfs(board, i, 0);
26                 dfs(board, i, 0);
27             }
28
29             if(board[i][n-1] == 'O'){
30                 // bfs(board, i, n-1);
31                 dfs(board, i, n-1);
32             }
33         }
34
35         for(int i = 0; i<m; i++){
36             for(int j = 0; j<n; j++){
37                 if(board[i][j] == 'O'){
38                     board[i][j] = 'X';
39                 }
40
41                 if(board[i][j] == '#'){
42                     board[i][j] = 'O';
43                 }
44             }
45         }
46     }
47
48     private void dfs(char [][] board, int i ,int j){
49         if(i<0 || i>=board.length || j<0 || j>=board[0].length || board[i][j]!='O'){
50             return;
51         }
52
53         board[i][j] = '#';
54         for(int [] dir : dirs){
55             dfs(board, i+dir[0], j+dir[1]);
56         }
57     }
58
59     private void bfs(char [][] board, int i, int j){
60         int m = board.length;
61         int n = board[0].length;
62
63         if(i<0 || i>=m || j<0 || j>=n){
64             return;
65         }
66
68         board[i][j] = '#';
70
71         while(!que.isEmpty()){
72             int cur = que.poll();
73             int p = cur/n;
74             int q = cur%n;
75
76             for(int [] dir : dirs){
77                 int xp = p + dir[0];
78                 int xq = q + dir[1];
79                 if(xp>=0 && xp<m && xq>=0 && xq<n && board[xp][xq] == 'O'){
80                     board[xp][xq] = '#';