DFS + Maximum number of connected subgraphs 200. Number of islands +130. Surrounded area

200. Number of islands

Give you a two-dimensional grid composed of '1' (land) and '0' (water), please count the number of islands in the grid.

Islands are always surrounded by water, and each island can only be formed by horizontal and / or vertical adjacent land connections.

In addition, you can assume that all four sides of the grid are surrounded by water.

Example 1:

Enter:

11110
11010
11000
00000
输出: 1

Example 2:

Enter:

11000
11000
00100
00011


输出: 3

Explanation: Each island can only be formed by connecting adjacent lands in horizontal and / or vertical directions.

Source: LeetCode (LeetCode)
Link: https://leetcode-cn.com/problems/number-of-islands The
copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

Problem-solving
Depth-first traversal of each subgraph, marked as called, calling DFS several times indicates that there are several subgraphs;

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        //计算最大连通子图的数量
        for(int i=0;i<grid.size();i++)
            for(int j=0;j<grid[i].size();j++)
                if(grid[i][j]=='1')
                {
                    DFS(grid,i,j);
                    count++;
                }
        return count;
    }

private:
    int count=0;
    int xy[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

    void DFS(vector<vector<char>> &grid,int x,int y)
    {   
        grid[x][y]='0';    //标记此点已遍历
        
        for(int i=0;i<4;i++)
        {
            int  x1=x+xy[i][0];
            int  y1=y+xy[i][1];
            if(x1>=0&&x1<grid.size()&&y1>=0&&y1<grid[x1].size()&& grid[x1][y1]=='1')      
                        DFS(grid,x1,y1);
        }

    }
};

130. Surrounded area

Given a two-dimensional matrix, contains 'X' and 'O' (letter O).

Find all the areas surrounded by 'X', and fill all 'O' in these areas with 'X'.

Example:

X X X X
X O O X
X X O X
X O X X

After running your function, the matrix becomes:

X X X X
X X X X
X X X X
X O X X

Explanation:

The enclosed interval will not exist on the boundary, in other words, any 'O' on the boundary will not be filled with 'X'. Any 'O' that is not on the boundary, or not connected to the 'O' on the boundary will eventually be filled with 'X'. If two elements are adjacent in the horizontal or vertical direction, they are said to be "connected".

Source: LeetCode
Link: https://leetcode-cn.com/problems/surrounded-regions
Copyright belongs to the deduction network. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

Solve the problem
First traverse the 'O' connected subgraphs starting from the periphery, and traverse the entire graph after recording, the part that overlaps with the above does not change, and the part that does not overlap changes to 'X'

class Solution {
public:
    void solve(vector<vector<char>>& board) {

        if(board.size()==0) return;
        vector<vector<char>> tmp(board);
        for(int i=0;i<board.size();i++)
            {
                if(tmp[i][0]=='O')
                    DFS(tmp,i,0);
                if(tmp[i][tmp[i].size()-1]=='O')
                    DFS(tmp,i,tmp[i].size()-1);
            }                
        for(int i=0;i<board[0].size();i++){  
                if(tmp[0][i]=='O')
                    DFS(tmp,0,i);
                if(tmp[tmp.size()-1][i]=='O')
                    DFS(tmp,tmp.size()-1,i);
        }
        for(int i=0;i<tmp.size();i++)
            for(int j=0;j<tmp[i].size();j++)
                if(tmp[i][j]=='O')
                    board[i][j]='X';
    }

private:
    //任何不在边界上,或不与边界上的 'O' 相连的 'O' 最终都会被填充为 'X'。
    //水平和垂直相连
    int xy[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

    void DFS(vector<vector<char>> &tmp,int x,int y)
    {
        tmp[x][y]='X';

        for(int i=0;i<4;i++)
        {
            int x1=x+xy[i][0],y1=y+xy[i][1];
            if(x1>=0&&x1<tmp.size()&&y1>=0&&y1<tmp[x1].size()&&tmp[x1][y1]=='O')
                DFS(tmp,x1,y1);
        }
    }    
};
Published 105 original articles · won praise 6 · views 4936

Guess you like

Origin blog.csdn.net/BLUEsang/article/details/105632768