LeetCode[BFS]: 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

参考:https://oj.leetcode.com/discuss/9942/my-bfs-solution-c-28ms

采取逆向思维,不是找到所有被’X’包围的’O’,而是将所有未被包围的’O’先标记为’#’,然后将剩余的’O’标记为’X’,将’#’标记为’O’,即可达到想要的目的。

C++代码实现如下:

class Solution {
public:
    void solve(vector<vector<char>> &board) {
        int height = board.size();
        if (height == 0) return;
        int width = board[0].size();
        if (width == 0) return;

        for (int i = 0; i < height; ++i) {
            if (board[i][0] == ‘O‘) BFSBoundary(board, i, 0);
            if (board[i][width - 1] == ‘O‘) BFSBoundary(board, i, width - 1);
        }

        for (int j = 1; j < width - 1; ++j) {
            if (board[0][j] == ‘O‘) BFSBoundary(board, 0, j);
            if (board[height - 1][j] == ‘O‘) BFSBoundary(board, height - 1, j);
        }

        for (int i = 0; i < height; ++i) {
            for (int j = 0; j < width; ++j) {
                if (board[i][j] == ‘O‘) board[i][j] = ‘X‘;
                if (board[i][j] == ‘#‘) board[i][j] = ‘O‘;
            }
        }
    }

private:
    void BFSBoundary(vector<vector<char>> &board, int h, int w) {
        int height = board.size();
        int width = board[0].size();

        queue<pair<int, int>> q;
        q.push(make_pair(h, w));
        board[h][w] = ‘#‘;

        while (!q.empty()) {
            pair<int, int> point = q.front();
            q.pop();

            pair<int, int> neighbors[4] = { { point.first + 1, point.second },
                                            { point.first - 1, point.second },
                                            { point.first, point.second + 1 },
                                            { point.first, point.second - 1 } };

            for (auto neig : neighbors) {
                if ((neig.first >= 0 && neig.first < height)
                    && (neig.second >= 0 && neig.second < width)
                    && (board[neig.first][neig.second] == ‘O‘)){
                    q.push(make_pair(neig.first, neig.second));
                    board[neig.first][neig.second] = ‘#‘;
                }
            }
        }
    }
};
时间: 2024-08-13 07:47:20

LeetCode[BFS]: Surrounded Regions的相关文章

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】 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][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

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 130 Surrounded Regions(BFS)

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][Java] 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 sho

【leetcode】Surrounded Regions(middle)☆

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

Java for LeetCode 130 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 XX O O XX X O XX O X X After running your function, the board should be: