day-63 Code Caprice Algorithm Training Camp (19) Graph Theory Part 02

1020. Number of enclaves

Analysis: Find the number of landmasses that are not bordered by borders
Idea 1: Depth-first traversal
  • First look for land from the four sides, and then perform a depth-first traversal to convert all adjacent land (1) into oceans (0)
    • Depth-first traversal : recursive traversal from four directions
  • Traverse the entire graph and count the number of all land masses.
class Solution {
public:
    int direct[4][2]={
   
   {0,1},{0,-1},{1,0},{-1,0}};
    int res=0;
    void dfs(vector<vector<int>>&grid,int x,int y){
        grid[x][y]=0;
        for(int i=0;i<4;i++){
            int nextx=x+direct[i][0];
            int nexty=y+direct[i][1];
            if(nextx>=0 && nextx<grid.size() && nexty>=0 && nexty<grid[0].size()){//边界条件
                if(grid[nextx][nexty]==1){
                    grid[nextx][nexty]=0;
                    dfs(grid,nextx,nexty);
                }
            }
        }
    }
    int numEnclaves(vector<vector<int>>& grid) {
        int n=grid.size(),m=grid[0].size();
        for(int i=0;i<n;i++){
            if(grid[i][0]==1) dfs(grid,i,0);//左侧边
            if(grid[i][m-1]==1) dfs(grid,i,m-1);//右侧边
        }
        for(int j=0;j<m;j++){
            if(grid[0][j]==1) dfs(grid,0,j);//上侧边
            if(grid[n-1][j]==1) dfs(grid,n-1,j);//下侧边
        }
        for(int i=1;i<n-1;i++){//遍历整个图
            for(int j=1;j<m-1;j++){
                if(grid[i][j]==1) res++;
            }
        }
        return res;
    }
};

130. Surrounded area

Idea 1: dfs
  • Still traverse the land depth first from four sides, and then change it to the A character
  • Then traverse the entire graph, changing the remaining land (which must be wrapped by sea water) into sea water, and the A character into land
class Solution {
public:
    int direct[4][2]={
   
   {0,1},{0,-1},{1,0},{-1,0}};
    int res=0;
    void dfs(vector<vector<char>>&board,char target,int x,int y)
    {
        board[x][y]=target;
        res++;
        for(int i=0;i<4;i++){
            int nextx=x+direct[i][0];
            int nexty=y+direct[i][1];
            if(nextx>=0 && nextx<board.size() && nexty>=0 && nexty<board[0].size()){
                if(board[nextx][nexty]=='O'){
                    board[nextx][nexty]=target;
                    dfs(board,target,nextx,nexty);
                }
            }
        }
    }
    void solve(vector<vector<char>>& board) {

        int n=board.size(),m=board[0].size();
        for(int i=0;i<n;i++){
            if(board[i][0]=='O') dfs(board,'A',i,0);//左侧边
            if(board[i][m-1]=='O') dfs(board,'A',i,m-1);//右侧边
        }
        for(int j=0;j<m;j++){
            if(board[0][j]=='O') dfs(board,'A',0,j);//上侧边
            if(board[n-1][j]=='O') dfs(board,'A',n-1,j);//下侧边
        }
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(board[i][j]=='A') board[i][j]='O';//所有的A变为O
                else if(board[i][j]=='O') board[i][j]='X';//所有的O变为X
            }
        } 
    }
};

417. Pacific-Atlantic water flow problem

Idea 1: Depth-first traversal
  • Two arrays are obtained from the Atlantic and Pacific sides respectively.
  • When both arrays pass through the same position, it means they can flow to both sides.
class Solution {
public:
    int direct[4][2]={
   
   {1,0},{-1,0},{0,1},{0,-1}};
    void dfs(vector<vector<int>>&heights,vector<vector<bool>>&visted,int x,int y){
        if(visted[x][y]) return;
        visted[x][y]=true;
        for(int i=0;i<4;i++){
            int nextx=x+direct[i][0];
            int nexty=y+direct[i][1];
            if(nextx>=0 && nextx<heights.size() && nexty>=0 && nexty<heights[0].size()){
                if(heights[x][y]<=heights[nextx][nexty])//本来是从高到低,这是倒着推,所以低到高
                    dfs(heights,visted,nextx,nexty);
            }
        }
    }
    vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {
        int n=heights.size(),m=heights[0].size();
        vector<vector<int>>res;

        vector<vector<bool>>pacific(n,vector<bool>(m,false));//太平洋
        vector<vector<bool>>atlantic(n,vector<bool>(m,false));//大西洋

        for(int i=0;i<n;i++){
            dfs(heights,pacific,i,0);//从左侧太平洋出发
            dfs(heights,atlantic,i,m-1);//从右侧大西洋出发
        }

        for(int j=0;j<m;j++){
            dfs(heights,pacific,0,j);//从上侧太平洋出发
            dfs(heights,atlantic,n-1,j);//从下侧大西洋出发
        }

        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(pacific[i][j] && atlantic[i][j])//从大西洋和太平洋都可以流过
                    res.push_back({i,j});
            }
        }
        return res;
    }
};

Guess you like

Origin blog.csdn.net/Ricardo_XIAOHAO/article/details/133444385