当前位置:网站首页>130. surrounding area

130. surrounding area

2022-06-24 18:33:00 One star accompanies the moon

Power button 2020/8/11 Punch in topic
This topic is similar to the maze , If you can't get to the boundary of the map , Mark all possible routes , If you can reach the border, you don't have to deal with it . This is obviously a search (dfs and bfs All right )
O Representative channel ,X Representative wall
If you do a pop search, most of the time TLE, The size of the graph is unknown . It must be very slow to judge point by point , It's a question , Go to the border , That's the point , We can start from the boundary , Then you just need to start with all the boundaries , Mark all the paths that can be reached !

class Solution {
    
public:
    int dx[4]={
    1,0,-1,0};
    int dy[4]={
    0,1,0,-1};
    int row,col;
    void dfs(vector<vector<char>>& board,int x,int y){
    
        if(x<0||x>=row||y<0||y>=col||board[x][y]!='O') return;
        board[x][y]='Y';
        for(int i=0;i<4;i++){
    
            int xx=x+dx[i];
            int yy=y+dy[i];
            dfs(board,xx,yy);
        }
    }
    void solve(vector<vector<char>>& board) {
    
        row=board.size();
        if(row==0) return;
        col=board[0].size();
        if(col==0) return;
        for(int i=0;i<row;i++){
    
            dfs(board,i,0);
            dfs(board,i,col-1);
        }
        for(int i=1;i<col-1;i++){
    
            dfs(board,0,i);
            dfs(board,row-1,i);
        }
        for(int i=0;i<row;i++){
    
            for(int j=0;j<col;j++){
    
                if(board[i][j]=='Y') board[i][j]='O';
                else board[i][j]='X';
            }
        }
    }
};
原网站

版权声明
本文为[One star accompanies the moon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211333580600.html