leetcoe || 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

Hide Tags

Breadth-first Search

题意:将被X包围的O换成X

thinking:

(1)首先我用递归做的,提交栈溢出了....

(2)参考网上的实现,要用到一种图形学的算法:泛洪算法-维基

采用BFS或者DFS,使用queue或者stack结构。

code:

class Solution {
public:
    int row;
    int col;

    void bfs(vector<vector<char> > &board, int r, int c) {
        queue<pair<int, int> > qe;
        qe.push({r, c});

        while(!qe.empty()) {
            r = qe.front().first;
            c = qe.front().second;
            qe.pop();
            if (r>0&&board[r-1][c]=='O') {
                board[r-1][c] = '+';
                qe.push({r-1,c});
            }
            if (r+1<row&&board[r+1][c]=='O') {
                board[r+1][c] = '+';
                qe.push({r+1,c});
            }
            if (c>0&&board[r][c-1]=='O') {
                board[r][c-1] = '+';
                qe.push({r,c-1});
            }
            if (c+1<col&&board[r][c+1]=='O') {
                board[r][c+1] = '+';
                qe.push({r,c+1});
            }
        }
        return;
    }

    void solve(vector<vector<char> > &board) {
        if (board.empty())
            return;

        row = board.size();
        col = board[0].size();        

        // top edge
        for(int i = 0; i < col; ) {
            if(board[0][i] == 'O') {
                int j=i;
                while(board[0][j]=='O') j++;
                board[0][i] = '+';
                bfs(board, 0, i);
                i = j;
            } else
                i++;
        }

        // bottom edge
        for(int i = 0; i < col;) {
            if(board[row-1][i] == 'O') {
                int j=i;
                while(board[row-1][j]=='O') j++;
                board[row-1][i] = '+';
                bfs(board, row-1, i);
                i = j;
            } else
                i++;
        }

        // left edge
        for(int i = 1; i < row-1;) {
            if(board[i][0] == 'O') {
                int j=i;
                while(board[j][0]=='O') j++;
                board[i][0] = '+';
                bfs(board, i, 0);
                i = j;
            } else
                i++;
        }

        // right edge
        for(int i = 1; i < row-1; ) {
            if(board[i][col-1] == 'O') {
                int j=i;
                while(board[j][col-1]=='O') j++;
                board[i][col-1] = '+';
                bfs(board, i, col-1);
                i = j;
            } else
                i++;
        }

        // then scan all the cells, recover live cells and flip dead cells
        for(int i = 0; i < row; i++)
            for(int j = 0; j < col; j++)
                board[i][j] = (board[i][j]=='+')?'O':'X';

        return;
    }
};
时间: 2024-10-15 18:18:18

leetcoe || 130、Surrounded Regions的相关文章

LeetCode(130) Surrounded Regions

题目 Given a 2D board containing 'X' and 'O' (the letter 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 --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 [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、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 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: 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 XX O O XX X O XX O X XAfter running your function, the

[LintCode] 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.Example X X X XX O O XX X O XX O X X After capture all regions surrounded by 'X', the boar

[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