Surrounded Regions -- leetcode

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

基本思路:

以矩阵最外层的每个点,如果其值为‘O‘,进行bfs搜索,并将访问过的结点,从‘O‘,变为其他字符‘1‘,以作为标记。

1.是作为已访问标记。2.是作为连通标记。

当对外层所有节点完成上述操作后的。

再进行一次全体扫描,其中,如果值仍为‘O‘,说明这些是被‘X‘包围的结点,不与边界上的‘O‘联通。则将其置为‘X‘。

如果其值为‘1‘,说明是刚才bfs搜索过的节点,它们并未被‘X‘全部包围。则恢复其值为‘O‘。

在leetcode上实际执行时间为20ms.

class Solution {
public:
    void solve(vector<vector<char>>& board) {
        if (board.empty()) return;
        const int row = board.size();
        const int col = board[0].size();
        const char C = '1';
        for (int i=0; i<row; i++) {
            for (int j=0; j<col; j++) {
                if (i && j && i!=row-1 && j!=col-1 || board[i][j]=='X')
                    continue;

                queue<pair<int, int> >q;
                q.push(make_pair(i, j));
                board[i][j] = C;
                while (!q.empty()) {
                    int count = q.size();
                    while (count--) {
                        auto xy = q.front();
                        q.pop();
                        if (xy.first && board[xy.first-1][xy.second]=='O') {
                            board[xy.first-1][xy.second] = C;
                            q.push(make_pair(xy.first-1, xy.second));
                        }
                        if (xy.second && board[xy.first][xy.second-1]=='O') {
                            board[xy.first][xy.second-1] = C;
                            q.push(make_pair(xy.first, xy.second-1));
                        }
                        if (xy.first+1<row && board[xy.first+1][xy.second]=='O') {
                            board[xy.first+1][xy.second] = C;
                            q.push(make_pair(xy.first+1, xy.second));
                        }
                        if (xy.second+1<col && board[xy.first][xy.second+1]=='O') {
                            board[xy.first][xy.second+1] = C;
                            q.push(make_pair(xy.first, xy.second+1));
                        }
                    }
                }
            }
        }

        for (int i=0; i<row; i++) {
            for (int j=0; j<col; j++) {
                if (board[i][j]=='O')
                    board[i][j] = 'X';
                else if (board[i][j]==C)
                    board[i][j] = 'O';
            }
        }
    }
};
时间: 2024-11-07 11:34:31

Surrounded Regions -- leetcode的相关文章

Surrounded Regions leetcode java

题目: 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 sho

Surrounded Regions LeetCode :My Solution

Surrounded Regions Total Accepted: 14948 Total Submissions: 105121My Submissions 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 exampl

Surrounded Regions——LeetCode

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

LeetCode OJ - Surrounded Regions

我觉得这道题和传统的用动规或者贪心等算法的题目不同.按照题目的意思,就是将被'X'围绕的'O'区域找出来,然后覆盖成'X'. 那问题就变成两个子问题: 1. 找到'O'区域,可能有多个区域,每个区域'O'都是相连的: 2. 判断'O'区域是否是被'X'包围. 我采用树的宽度遍历的方法,找到每一个'O'区域,并为每个区域设置一个value值,为0或者1,1表示是被'X'包围,0则表示不是.是否被'X'包围就是看'O'区域的边界是否是在2D数组的边界上. 下面是具体的AC代码: class Boar

leetcode --day12 Surrounded Regions &amp; Sum Root to Leaf Numbers &amp; Longest Consecutive Sequence

1.  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 fu

[LeetCode][JavaScript]Surrounded Regions

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

【LeetCode】 Surrounded Regions (BFS &amp;&amp; DFS)

题目:Surrounded Regions 广搜和深搜都能解决,但是LeetCode上使用深搜时会栈溢出 DFS: <span style="font-size:18px;">/*LeetCode Surrounded Regions * 题目:给定一个字符数组,由'X'和'O'组成,找到所有被x包围的o并将其替换为x * 思路:只要替换被包围的o就行,如果有一个o是边界或者上下左右中有一个是o且这个o不会被替换,则该点也不会被替换 * 从四条边开始,因为在这4周的一定不是

LeetCode: Surrounded Regions [130]

[题目] 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 sh

验证LeetCode Surrounded Regions 包围区域的DFS方法

在LeetCode中的Surrounded Regions 包围区域这道题中,我们发现用DFS方法中的最后一个条件必须是j > 1,如下面的红色字体所示,如果写成j > 0的话无法通过OJ,一直百思不得其解其中的原因,直到有网友告诉我说他验证了最后一个大集合在本地机子上可以通过,那么我也来验证看看吧. class Solution { public: void solve(vector<vector<char> >& board) { for (int i =