[leetcode] 130.Surrounded Regions

原创 2015年07月07日 14:41:24

题目:
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
题意:
给一个二维的表格,里面包含了’X’和’O’,找出所有被X包围的范围,将里面的O改成O。
思路:
这个题目有点像下围棋的感觉,我们不妨可以反过来思考这个问题。题目让找出被X包围的O并将其改为X。我们不妨找出那些不能被修改的O,不能被包围的部分是与四个边上的O相邻的O,相邻的意思是这两个点的位置处于上下位置或者左右位置。如果不能从四个边到达的O一定是被X包围起来的,所以我们的目标就变成了找出所有与四个边上的O相邻的所有 O,然后把它们标记为‘+’这类符号,把剩下的O全部改成X即可。
代码如下:
采用广度优先搜索,把所有的四个边上的O的位置找出来,并把这些O改为+,接下来通过广度优先搜索的方法找出其他的O。

class Solution {
public:
    void solve(vector<vector<char>>& board) {
        int row = board.size();
        if(row == 0)return;
        int col = board[0].size();
        if(col == 0)return;
        queue<pair<int,int>> OIndex;
        for(auto i = 0; i < col; i++) {
            if(board[0][i] == 'O') {
                OIndex.push(make_pair(0, i));
                board[0][i] = '+';
            }
            if(board[row - 1][i] == 'O') {
                OIndex.push(make_pair(row-1, i));
                board[row - 1][i] = '+';
            }
        }

        for(auto i = 0; i < row; i++) {
            if(board[i][0] == 'O') {
                OIndex.push(make_pair(i, 0));
                board[i][0] = '+';
            }
            if(board[i][col - 1] == 'O') {
                OIndex.push(make_pair(i, col - 1));
                board[i][col - 1] = '+';
            }
        }

        while(!OIndex.empty()) {
            pair<int,int> index = OIndex.front();
            OIndex.pop();
            if(index.first != 0 && board[index.first - 1][index.second] == 'O') {
                board[index.first - 1][index.second] = '+';
                OIndex.push(make_pair(index.first - 1, index.second));
            }
            if(index.first != row - 1 && board[index.first + 1][index.second] == 'O') {
                board[index.first + 1][index.second] = '+';
                OIndex.push(make_pair(index.first + 1, index.second));
            }
            if(index.second != 0 && board[index.first][index.second - 1] == 'O') {
                board[index.first][index.second - 1] = '+';
                OIndex.push(make_pair(index.first, index.second - 1));
            }
            if(index.second != col - 1 && board[index.first][index.second + 1] == 'O') {
                board[index.first][index.second + 1] = '+';
                OIndex.push(make_pair(index.first, index.second + 1));
            }
        }
        for(auto i = 0; i < row; i++)
            for(auto j = 0; j < col; j++){
                if(board[i][j] == 'O')board[i][j] = 'X';
                else if(board[i][j] == '+')board[i][j] = 'O';
            }
    }
};
版权声明:本文为博主原创文章,未经博主允许不得转载。 举报

相关文章推荐

LeetCode 130. Surrounded Regions (并查集)

LeetCode 130. Surrounded Regions (并查集)LeetCode 130 Surrounded Regions 并查集 问题描述 解题思路 参考代码 Tags: - B...

[Leetcode] 130. Surrounded Regions 解题报告

题目: Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by...

我是如何成为一名python大咖的?

人生苦短,都说必须python,那么我分享下我是如何从小白成为Python资深开发者的吧。2014年我大学刚毕业..

(LeetCode 130)Surrounded Regions(并查集)

Q: Given a 2D board containing ‘X’ and ‘O’, capture all regions surrounded by ‘X’.A region is captu...

leetcode——130——Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captur...

Leetcode 130 Surrounded Regions

Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X&#...

LeetCode 130. Surrounded Regions

逆向思维,从边缘的O出发(上下左右四排的O),进行DFS, 将这些'O'可达的所有点'O'都标记为'Z'(包括它们自身也标记为O), 这些'Z'最终都可以保持为'O'不变,而其他点应该都变成'X'. .....

利用BFS和DFS解决 LeetCode 130: Surrounded Regions

问题来源此题来源于LEETCODE,具体问题详见下面的链接 https://leetcode.com/problems/surrounded-regions/description/问题简述给定一个...

[leetcode] 130. Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captur...

[leetcode 130] Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap...

Leetcode 130. Surrounded Regions

130. Surrounded Regions Total Accepted: 53520 Total Submissions: 330327 Difficulty: Medium ...
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)