[leetcode] 130. Surrounded Regions

本文详细解析了在二维棋盘上围攻由‘X’包围的‘O’区域的算法,通过BFS和DFS两种方法实现,最终将被围攻的‘O’转化为‘X’。介绍了算法的具体实现步骤,并提供了代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

Explanation:
Surrounded regions shouldn’t be on the border, which means that any ‘O’ on the border of the board are not flipped to ‘X’. Any ‘O’ that is not on the border and it is not connected to an ‘O’ on the border will be flipped to ‘X’. Two cells are connected if they are adjacent cells connected horizontally or vertically.

解法1:BFS

找到边界上的O,然后使用一个queue进行宽搜,找出所有的连接边界O的点。
自己的标记方法:visited表示是否访问,connetO表示是否连接O;
小chick:找到之后将O变为$,然后将所有的O变成X,再将所有的 $变为O。

class Solution {
public:
    void solve(vector<vector<char>>& board) {
        if(board.empty() || board[0].empty())
            return ;
        int row = board.size(), col = board[0].size();
        vector<vector<bool>> visited(row, vector<bool> (col, 0));
        vector<vector<bool>> connectO(row, vector<bool> (col, 0));
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if((i==0 || j==0 || i==row-1 || j==col-1) && board[i][j] == 'O' && visited[i][j] == 0){
                    queue<int> q;
                    q.push(i*col+j);
                    while(!q.empty()){
                        int t = q.front(), x = t / col, y = t % col; q.pop();
                        cout<<x<<" "<<y<<endl;
                        connectO[x][y] = 1;
                        visited[x][y] = 1;
                        if(x-1 >= 0 && visited[x-1][y]==0 && board[x-1][y]=='O'){
                            q.push(t-col);
                            visited[x-1][y] = 1;
                        }
                        if(y+1 <col && visited[x][y+1]==0 && board[x][y+1]=='O'){
                            q.push(t+1);
                            visited[x][y+1] = 1;
                        }
                            
                        if(x+1 <row && visited[x+1][y]==0 && board[x+1][y]=='O'){
                            q.push(t+col);
                            visited[x+1][y] = 1;
                        }
                            
                        if(y-1 >= 0 && visited[x][y-1]==0 && board[x][y-1]=='O'){
                            q.push(t-1);
                            visited[x][y-1] = 1;
                        }
                            
                    }
                }
            }
        }
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(connectO[i][j] == 0)
                    board[i][j] = 'X';
            }
        }
    }
};
class Solution {
public:
    void solve(vector<vector<char>>& board) {
        if(board.empty() || board[0].empty())
            return ;
        int row = board.size(), col = board[0].size();
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(i!=0 && j!=0 && i!=row-1 && j!=col-1) 
                   continue;
                if (board[i][j] != 'O') continue;
                board[i][j] = '$';
                queue<int> q;
                q.push(i*col+j);
                while(!q.empty()){
                    int t = q.front(), x = t / col, y = t % col; q.pop();
                    if(x-1 >= 0 && board[x-1][y]=='O'){
                        q.push(t-col);
                        board[x-1][y] = '$';
                    }
                    if(y+1 <col && board[x][y+1]=='O'){
                        q.push(t+1);
                        board[x][y+1] = '$';
                    }

                    if(x+1 <row && board[x+1][y]=='O'){
                        q.push(t+col);
                        board[x+1][y] = '$';
                    }

                    if(y-1 >= 0 && board[x][y-1]=='O'){
                        q.push(t-1);
                        board[x][y-1] = '$';
                    }

                }
            }
        }
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(board[i][j] == 'O') board[i][j] = 'X';
                if(board[i][j] == '$') board[i][j] = 'O';
            }
        }
    }
};

解法2:DFS

class Solution {
public:
    void solve(vector<vector<char>>& board) {
        if(board.empty() || board[0].empty())
            return ;
        int row = board.size(), col = board[0].size();
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(i!=0 && j!=0 && i!=row-1 && j!=col-1) 
                   continue;
                if (board[i][j] != 'O') continue;
                solveDFS(board, i, j);
            }
        }
        for (int i = 0; i < row; ++i) {
            for (int j = 0; j < col; ++j) {
                if (board[i][j] == 'O') board[i][j] = 'X';
                if (board[i][j] == '$') board[i][j] = 'O';
            }
        }
    }
    
    void solveDFS(vector<vector<char>>& board, int i, int j){
        board[i][j] = '$';
        if (i > 0 && board[i - 1][j] == 'O') 
            solveDFS(board, i - 1, j);
        if (j < board[i].size() - 1 && board[i][j + 1] == 'O') 
            solveDFS(board, i, j + 1);
        if (i < board.size() - 1 && board[i + 1][j] == 'O') 
            solveDFS(board, i + 1, j);
        if (j > 1 && board[i][j - 1] == 'O') 
            solveDFS(board, i, j - 1);
    }
       
};

参考

https://www.cnblogs.com/grandyang/p/4555831.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值