Surrounded Regions

原创 2015年11月18日 18:03:34

题目: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改变成X,而是先把最外围的O变成另外一个符号,最后把其他符号均变成X,而把刚刚的那个改成O。

接下来就是BFS还是DFS了。

DFS写在代码里面,但是会出现栈溢出。

BFS似乎不会,我不知道为啥,只是能够成功。

具体的概念就是一旦在四周遇到O,那么存入堆栈,再插入周围的四个方向的。但是要在堆栈循环里面判断i和j是否满足数组范围条件啥的以及本身是否是O。

程序最后就是把其他的负号全部变成X,我所改变的那个负号变成O。即可。

代码:

class Solution {
public:
/*
    void solve(vector<vector<char>>& board) {
        if(board.empty()||board[0].empty()) return;
        int i=0,j=0;
        int m=board.size(),n=board[0].size();
        
        for(;j<n-1;j++){ 
            walk(board,i,j);
        }
        
        for(;i<m-1;i++){        //right
            walk(board,i,j);
        }
        
        for(;j>0;j--){          //down
            walk(board,i,j);
        }
        
        for(;i>0;i--){//left
            walk(board,i,j);
        }
        
        for(i=0;i<m;i++){
            for(j=0;j<n;j++){
                if(board[i][j]=='#') board[i][j]='O';
                else    board[i][j]='X';
            }
        }
    }
    
    void walk(vector<vector<char> >&board,int i,int j){
        //DFS
        if(i<0||i>=board.size()||j<0||j>=board[0].size())   return;
        if(board[i][j]=='O'){
            board[i][j]='#';
            walk(board,i-1,j);
            walk(board,i+1,j);
            walk(board,i,j-1);
            walk(board,i,j+1);
        }
    }*/
    
        void bfs(int i, int j, vector<vector<char>>& board) {
        int n = board.size();
        int m = board[0].size();
        queue<pair<int, int>> q;
        q.push(make_pair(i, j));
        while (!q.empty()) {
            auto top = q.front();
            q.pop();
            if (top.first >= 0 && top.first < n && top.second >= 0 && top.second < m && board[top.first][top.second] == 'O') {
                //如果没有这句话,那么你接下来的make_pair就会出错,溢出.
                board[top.first][top.second] = '#';
                q.push(make_pair(top.first - 1, top.second));
                q.push(make_pair(top.first + 1, top.second));
                q.push(make_pair(top.first, top.second + 1));
                q.push(make_pair(top.first, top.second - 1));
            }
        }
    }

    void solve(vector<vector<char>>& board) {
        if(board.empty()||board[0].empty()) return;
        int m=board.size(),n=board[0].size();
        
        for(int i=0;i<m;i++){
            //最左边与最右边
            if(board[i][0]=='O')    walk( board,i,0);
            if(board[i][n-1]=='O')  walk( board,i,n-1);
        }
        
        for(int i=1;i<n-1;i++){
            //最上边与最下边
            if(board[0][i]=='O')    walk( board,0,i);
            if(board[m-1][i]=='O')  walk( board,m-1,i);
        }
        
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(board[i][j]=='#')    board[i][j]='O';
                else    board[i][j]='X';
            }
        }
    }
    
    void walk(vector<vector<char>>& board,int i,int j){
        int m=board.size(),n=board[0].size();
        queue<pair<int,int>>q;
        q.push(make_pair(i,j));
        while(!q.empty()){
            auto tem=q.front();
            q.pop();
            if(tem.first>=0&&tem.first<m&&tem.second>=0&&tem.second<=n&&board[tem.first][tem.second]=='O'){
                board[tem.first][tem.second]='#';
                q.push(make_pair(tem.first-1,tem.second));
                q.push(make_pair(tem.first+1,tem.second));
                q.push(make_pair(tem.first,tem.second-1));
                q.push(make_pair(tem.first,tem.second+1));
            }
        }
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

【leetcode题解】5 - Surrounded Regions

Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X...

LeetCode 130. Surrounded Regions (并查集)

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

leetcode Surrounded Regions

层次扫描结合广度优先搜索

Surrounded Regions-leetcode

题目: Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A regi...

[Leetcode] 130. Surrounded Regions 解题报告

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

(LeetCode 130)Surrounded Regions(并查集)

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

Leetcode Surrounded Regions 解题报告

http://oj.leetcode.com/problems/surrounded-regions/ Given a 2D board containing 'X' and 'O', captur...

leetcode-Surrounded Regions

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

leetCode_ Surrounded Regions(BFS)

题意:在由X和O组成的一个二维数组,把用X包围住的那些O改成X。 方法一:DFS(会超内存) 思想很简单,对于每一个还没有用过的O,把相邻的O全部找出来,并把这些x坐标,y坐标记录下来,如果这个O...

leetcode——130——Surrounded Regions

Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is captur...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Surrounded Regions
举报原因:
原因补充:

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