130. Surrounded Regions(M)

本文详细解析了一种解决二维棋盘上被'X'包围的'O'区域的问题算法。通过深度优先搜索(DFS),标记边缘可达的'O',避免其被错误地转换为'X'。最终将所有未被标记的'O'变为'X',实现区域围堵。

130.Add to List 130. Surrounded Regions

 1 Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'.
 2 
 3 A region is captured by flipping all 'O's into 'X's in that surrounded region.
 4 
 5 For example,
 6 X X X X
 7 X O O X
 8 X X O X
 9 X O X X
10 After running your function, the board should be:
11 
12 X X X X
13 X X X X
14 X X X X
15 X O X X
 

 

 1 /*
 2 60 / 60 test cases passed.
 3 Status: Accepted
 4 Runtime: 13 ms
 5 */
 6 
 7 class Solution {
 8 public:
 9     void dfs(vector<vector<char>>& board, int x, int y, int r, int c, vector<vector<bool>> &visited)
10     {
11         if(!visited[x][y] && board[x][y] == 'O')
12         {
13             visited[x][y] = true;
14             board[x][y] = '#';
15             if((x > 0) && (!visited[x-1][y]) && (board[x-1][y] == 'O')) dfs(board, x-1, y, r, c, visited); // top
16             if((x+1 < r) && (!visited[x+1][y]) && (board[x+1][y] == 'O')) dfs(board, x+1, y, r, c, visited); // bottom
17             if((y > 1) && (!visited[x][y-1]) && (board[x][y-1] == 'O')) dfs(board, x, y-1, r, c, visited); // left   ??? why "y > 0" can't pass the last test case.
18             if((y+1 < c) && (!visited[x][y+1]) && (board[x][y+1] == 'O')) dfs(board, x, y+1, r, c, visited); // right
19         }
20         return;
21     }
22 
23     void solve(vector<vector<char>>& board)
24     {
25         size_t rlen = board.size();
26         if(0 == rlen) return;
27         size_t clen = board[0].size();
28 
29         cout << board.size() << " " << board[0].size() << endl;
30         vector<vector<bool>> visited(rlen, vector<bool>(clen, false));
31 
32         for(int i=0; i<clen; i++) dfs(board, 0, i, rlen, clen, visited); // top
33         for(int j=0; j<clen; j++) dfs(board, rlen-1, j, rlen, clen, visited); // bottom
34         for(int m=0; m<rlen; m++) dfs(board, m, 0, rlen, clen, visited); // left
35         for(int n=0; n<rlen; n++) dfs(board, n, clen-1, rlen, clen, visited); // right
36 
37         for(int i = 0; i < rlen; i++)
38             for(int j = 0; j < clen; j++)
39             {
40                 if(board[i][j] == 'O')
41                     board[i][j] = 'X';
42                 if(board[i][j] == '#')
43                     board[i][j] = 'O';
44             }         
45     }
46 
47 };
View Code

 

 

 1 /*
 2 Concise 12ms C++ DFS solution
 3 https://discuss.leetcode.com/topic/45119/concise-12ms-c-dfs-solution
 4 */
 5 class Solution { //by Kenigma 
 6 public:
 7     void solve(vector<vector<char>>& board) {
 8         if (board.empty()) return;
 9         int row = board.size(), col = board[0].size();
10         for (int i = 0; i < row; ++i) {
11             check(board, i, 0);             // first column
12             check(board, i, col - 1);       // last column
13         }
14         for (int j = 1; j < col - 1; ++j) {
15             check(board, 0, j);             // first row
16             check(board, row - 1, j);       // last row
17         }
18         for (int i = 0; i < row; ++i)
19             for (int j = 0; j < col; ++j)
20                 if (board[i][j] == 'O') board[i][j] = 'X';
21                 else if (board[i][j] == '1') board[i][j] = 'O';
22     }
23     
24     void check(vector<vector<char>>& board, int i, int j) {
25         if (board[i][j] == 'O') {
26             board[i][j] = '1';
27             if (i > 1) check(board, i - 1, j);
28             if (j > 1) check(board, i, j - 1);
29             if (i + 1 < board.size()) check(board, i + 1, j);
30             if (j + 1 < board[0].size()) check(board, i, j + 1);
31         }
32     }
33 };
View Code

 

转载于:https://www.cnblogs.com/guxuanqing/p/6557996.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值