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和相连的O找出来就行了,其他部分都换写成X。
之前一直调用check会stack overflow。所以改用队列就过了。
class Solution {
public:
struct point
{
    int x;
    int y;
};
    vector <vector <bool>> bmap;
    queue <point> qp;
    void solve(vector<vector<char>> &board) {
        
        
        int x = board.size();
        if(x==0)return;
        int y = board[0].size();
        
        
        for(int i = 0 ; i < x ;i++)
        {
            vector<bool> bo(y,0);
            bmap.push_back(bo);
            
        }
        for(int i = 0 ; i < y ;i++)
        {
            if(bmap[0][i] == 0)
            {
                point pt;
                pt.x = 0;
                pt.y = i;
                qp.push(pt);
            }
            //check(0,i,board);
            if(bmap[x-1][i] == 0)
            {
                point pt;
                pt.x = x-1;
                pt.y = i;
                qp.push(pt);
            }
            //check(x-1,i,board);
        }
        for(int i = 1 ; i < x-1 ;i++)
        {
            if(bmap[i][0] == 0)
            {
                point pt;
                pt.x = i;
                pt.y = 0;
                qp.push(pt);
            }
            //check(i,0,board);
            if(bmap[i][y-1] == 0)
            {
                point pt;
                pt.x = i;
                pt.y = y-1;
                qp.push(pt);
            }
            //check(i,y-1,board);
        }
        while(!qp.empty())
        {
            point pt = qp.front();
            qp.pop();
            check(pt.x,pt.y,board);
        }
        
        for(int i = 0 ; i < y ; i++)
        for(int j = 0 ; j < x ;j++)
        {
            if(bmap[j][i] == 0)board[j][i] = 'X';
        }
    }
    void check(int x , int y , vector<vector<char>> &board)
    {
        bmap[x][y] = 1;
        if(board[x][y] == 'O' )
        {
            if(x -1 >=0 && bmap[x-1][y] == 0 &&board[x-1][y]=='O')
            {
                point pt;
                pt.x = x-1;
                pt.y = y;
                qp.push(pt);
            }
            //check(x-1,y,board);
            
            if(x +1 <board.size() && bmap[x+1][y] == 0 &&board[x+1][y]=='O')
            {
                point pt;
                pt.x = x+1;
                pt.y = y;
                qp.push(pt);
            }
            //check(x+1,y,board);
            
            if(y +1 <board[0].size() && bmap[x][y+1] == 0 &&board[x][y+1]=='O')
            {
                point pt;
                pt.x = x;
                pt.y = y+1;
                qp.push(pt);
            }
            //check(x,y+1,board);
            
            if(y -1 >=0 && bmap[x][y-1] == 0 &&board[x][y-1]=='O')
            {
                point pt;
                pt.x = x;
                pt.y = y-1;
                qp.push(pt);
            }
            //check(x,y-1,board);
            
        }
        
    }
};

  

原文地址:https://www.cnblogs.com/pengyu2003/p/3684109.html