LeetCode-Surrounded Regions

哎,做得比较郁闷的一道题,如果面试做这一题的话,肯定就跪了,一开始的时候思路就错了,只想着怎样把“O"变成"X",解的过程是遇到了"O"了再去判断,以dfs的方式遍历它四周的点,判断是否被"X"包围,如果被包围的话则改成"O"。只能过小数据,

大数据会在一个超长的输入那里出现Runtime Error。

后来到网上看了一下,发现正确的思路应该是用dfs或者bfs从外面一圈判断是否可连接到外面的”O",因为如果最外面一圈如果都是"X"的话,那么肯定是被包围的,所有的”O“都要被改成”X"。

DFS的代码很好写,不过奇怪的是我写完之后过不了大数据,可是看了一位同学的代码(http://blog.csdn.net/tuantuanls/article/details/8746458),和我写的基本一样,可是却可以过,而且她的代码中没有判断r < 0 || c < 0,竟然还能通过,感觉很诡异。后来我把bfs中的局部变量m = board.size(); n = board[0].size();去掉之后就能通过了!⊙﹏⊙b汗,这也行。。。

 1 class Solution {
 2 public:
 3     void dfs(vector<vector<char> > &board, int r, int c) {
 4         //int m = board.size();
 5         //int n = board[0].size();
 6         //if (r < 0 || c < 0 || r >= m || c >= n || board[r][c] != 'O') return;
 7         if (r < 0 || c < 0 || r >= board.size()  || c >= board[0].size() || board[r][c] != 'O') {
 8             return;
 9         }
10         board[r][c] = '#';
11         dfs(board, r, c + 1);
12         dfs(board, r, c - 1);
13         dfs(board, r + 1, c);
14         dfs(board, r - 1, c);
15     }
16     void solve(vector<vector<char> > &board) {
17         // Start typing your C/C++ solution below
18         // DO NOT write int main() function
19         int m = board.size();
20         if (m == 0) {
21             return;
22         }
23         int n = board[0].size();
24         for (int j = 0; j < n; j++) {
25             dfs(board, 0, j);
26             dfs(board, m - 1, j);
27         }
28         for (int i = 1; i < m - 1; i++) {
29             dfs(board, i, 0);
30             dfs(board, i, n - 1);
31         }
32         for (int i = 0; i < m; i++) {
33             for (int j = 0; j < n; j++) {
34                 if (board[i][j] == 'O') {
35                     board[i][j] = 'X';
36                 }
37                 if (board[i][j] == '#') {
38                     board[i][j] = 'O';
39                 }
40             }
41         }
42     }
43 };

用bfs的话可以一次顺利通过,队列我用的是C++ STL中的双端队列deque。

 1 class Solution {
 2 public:
 3     deque<int> around;
 4     void change(vector<vector<char> > &board, int r, int c) {
 5         if (r < 0 || c < 0 || r >= board.size()  || c >= board[0].size() || board[r][c] != 'O') {
 6             return;
 7         }
 8         board[r][c] = '1';
 9         around.push_back(r * board[0].size() + c);
10     }
11     void bfs(vector<vector<char> > &board, int r, int c) {
12         change(board, r, c);
13         while (!around.empty()) {
14             int pos = around.front();
15             int i = pos / board[0].size();
16             int j = pos % board[0].size();
17             change(board, i, j + 1);
18             change(board, i, j - 1);
19             change(board, i - 1, j);
20             change(board, i + 1, j);
21             around.pop_front();
22         }
23     }
24     void solve(vector<vector<char> > &board) {
25         // Start typing your C/C++ solution below
26         // DO NOT write int main() function
27         int m = board.size();
28         if (m == 0) {
29             return;
30         }
31         int n = board[0].size();
32         around.clear();
33         for (int j = 0; j < n; j++) {
34             bfs(board, 0, j);
35             bfs(board, m - 1, j);
36         }
37         for (int i = 1; i < m - 1; i++) {
38             bfs(board, i, 0);
39             bfs(board, i, n - 1);
40         }
41         for (int i = 0; i < m; i++) {
42             for (int j = 0; j < n; j++) {
43                 if (board[i][j] == 'O') {
44                     board[i][j] = 'X';
45                 }
46                 if (board[i][j] == '1') {
47                     board[i][j] = 'O';
48                 }
49             }
50         }
51     }
52 };
原文地址:https://www.cnblogs.com/chasuner/p/solve.html