leetcode -- Surrounded Regions

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

[解题思路]

 board的四个边上的O是无法被X包围的,故如果在四个边上发现O,则这些O应当被保留 从这些O出发继续寻找相邻的O,这些O也是要保留的

 等这些O都标记结束,则剩余的O就应该被改成X

1.使用DFS解,在过大数据集时会挂掉,200*200的矩阵会栈溢出,

 1 public class Solution {
 2     public static void solve(char[][] board) {
 3         if (board == null || board.length == 0) {
 4             return;
 5         }
 6         int row = board.length;
 7         int col = board[0].length;
 8 
 9         // up row
10         for (int j = 0; j < col; j++) {
11             if (board[0][j] == 'O') {
12                 dfs(board, 0, j);
13             }
14         }
15 
16         // bottom row
17         for (int j = 0; j < col; j++) {
18             if (board[row - 1][j] == 'O') {
19                 dfs(board, row - 1, j);
20             }
21         }
22 
23         // left column
24         for (int i = 0; i < row; i++) {
25             if (board[i][0] == 'O') {
26                 dfs(board, i, 0);
27             }
28         }
29 
30         // right column
31         for (int i = 0; i < row; i++) {
32             if (board[i][col - 1] == 'O') {
33                 dfs(board, i, col - 1);
34             }
35         }
36 
37         for (int i = 0; i < row; i++) {
38             for (int j = 0; j < col; j++) {
39                 if (board[i][j] == 'O') {
40                     board[i][j] = 'X';
41                 }
42                 if (board[i][j] == 'P') {
43                     board[i][j] = 'O';
44                 }
45             }
46         }
47     }
48 
49     private static void dfs(char[][] board, int i, int j) {
50         if(i < 0 || i >= board.length || j < 0 || j > board[0].length || board[i][j] != 'O'){
51             return;
52         }
53         board[i][j] = 'P';
54 
55         // up
56         if (i - 1 >= 0 && board[i - 1][j] == 'O') {
57             dfs(board, i - 1, j);
58         }
59         // bottom
60         if (i + 1 < board.length && board[i + 1][j] == 'O') {
61             dfs(board, i + 1, j);
62         }
63         // left
64         if (j - 1 >= 0 && board[i][j - 1] == 'O') {
65             dfs(board, i, j - 1);
66         }
67         // right
68         if (j + 1 < board[0].length && board[i][j + 1] == 'O') {
69             dfs(board, i, j + 1);
70         }
71 
72     }
73 }

 2.BFS

使用BFS来对board进行遍历,如果当前格是'O',则将该格位置放入到queue中

遍历queue,queue中当前格是'O'时,将其内容改为'P',则将该格的上下左右都放入到queue中,

 1 public class Solution {
 2     private Queue<Integer> queue = new LinkedList<Integer>();
 3     public void solve(char[][] board) {
 4         if (board.length == 0) {
 5             return;
 6         }
 7         if (board[0].length == 0) {
 8             return;
 9         }
10         int row = board.length;
11         int col = board[0].length;
12 
13         // up row
14         for (int j = 0; j < col; j++) {
15             if (board[0][j] == 'O') {
16                 bfs(board, 0, j);
17             }
18         }
19 
20         // bottom row
21         for (int j = 0; j < col; j++) {
22             if (board[row - 1][j] == 'O') {
23                 bfs(board, row - 1, j);
24             }
25         }
26 
27         // left column
28         for (int i = 0; i < row; i++) {
29             if (board[i][0] == 'O') {
30                 bfs(board, i, 0);
31             }
32         }
33 
34         // right column
35         for (int i = 0; i < row; i++) {
36             if (board[i][col - 1] == 'O') {
37                 bfs(board, i, col - 1);
38             }
39         }
40 
41         for (int i = 0; i < row; i++) {
42             for (int j = 0; j < col; j++) {
43                 if (board[i][j] == 'O') {
44                     board[i][j] = 'X';
45                 }
46                 if (board[i][j] == 'P') {
47                     board[i][j] = 'O';
48                 }
49             }
50         }
51 
52     }
53 
54     private void fill(char[][] board, int i, int j) {
55         int row = board.length;
56         int col = board[0].length;
57         if (i < 0 || i >= row || j < 0 || j >= col || board[i][j] != 'O')
58             return;
59 
60         queue.offer(i * col + j);
61         board[i][j] = 'P';
62     }
63 
64     private void bfs(char[][] board, int i, int j) {
65         int col = board[0].length;
66 
67         fill(board, i, j);
68         
69         while (!queue.isEmpty()) {
70             int cur = queue.poll();
71             int x = cur / col;
72             int y = cur % col;
73 
74             fill(board, x - 1, y);
75             fill(board, x + 1, y);
76             fill(board, x, y - 1);
77             fill(board, x, y + 1);
78         }
79     }
80 }

 

ref:

http://blog.sina.com.cn/s/blog_b9285de20101j1dt.html

你可能感兴趣的:(LeetCode)