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

思路:

一共是两种思路,不过本质都是一样,均是反其道而行,不进行正向的把里面的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));
            }
        }
    }
};


posted @ 2015-11-18 18:04  JSRGFJZ6  阅读(115)  评论(0编辑  收藏  举报