[Leetcode 106] 130 Surrounded Regions

Problem:

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

Analysis:

This is the first BFS problem I solved, congratuations! The basic idea is that we can search every position neighbouring a 'O'. If until reach the 'X' or the boundry of the board. If we reach the boundry, then implies these connected 'O's is not surrounded by 'X's. For other 'O's, we then change them to 'X'.

Carefully thinking about the process, many efforts are wasted, postions may be accessed multiple times. To reduce the workload, we can simply search the boundry, if a 'O' is encounted, we then conduct BFS. And we know that all the 'O's searched in this wat, should be 'O' and other remaining 'O's should be changed into 'X'.

Code:

 1 class Solution {
 2 public:
 3     int row, col;
 4 
 5     void solve(vector<vector<char>> &board) {
 6         // Start typing your C/C++ solution below
 7         // DO NOT write int main() function
 8         if (board.size() == 0)
 9             return ;
10         
11         row = board.size();
12         col = board[0].size();
13         
14         for (int i=0; i<col; i++) {
15             if (board[0][i] == 'O') {
16                 board[0][i] = 0;
17                 bfs(board, 0, i);
18             }
19             
20             if (board[row-1][i] == 'O') {
21                 board[row-1][i] = 0;
22                 bfs(board, row-1, i);
23             }
24         }
25         
26         for (int i=0; i<row; i++) {
27             if (board[i][0] == 'O') {
28                 board[i][0] = 0;
29                 bfs(board, i, 0);
30             }
31             
32             if (board[i][col-1] == 'O') {
33                 board[i][col-1] = 0;
34                 bfs(board, i, col-1);
35             }
36         }
37         
38         for (int i=0; i<row; i++)
39             for (int j=0; j<col; j++)
40                 if (board[i][j] == 'O')
41                     board[i][j] = 'X';
42                 else if (board[i][j] == 0)
43                     board[i][j] = 'O';
44     }
45     
46 private:
47     void bfs(vector<vector<char>> &board, int r, int c) {
48         int dirR[4] = {-1, 0, 1, 0};
49         int dirC[4] = {0, 1, 0, -1};
50         queue<int> q;
51         
52         q.push(r*row+c);
53         
54         while (!q.empty()) {
55             int v = q.front();
56             q.pop();
57             for (int i=0; i<4; i++) {
58                 int rr = v / row + dirR[i];
59                 int cc = v % row + dirC[i];
60                 if (isValid(rr, cc) && board[rr][cc] == 'O') {
61                     board[rr][cc] = 0;
62                     q.push(rr*row + cc);
63                 }
64             }
65         }
66     }
67     
68     bool isValid(int r, int c) {
69         return (r>=0 && r<row) && (c>=0 && c<col);
70     }
71 };
View Code
原文地址:https://www.cnblogs.com/freeneng/p/3263811.html