The number of T200 islands for LeetCode (medium)

The topic of this time is the number of T200 islands on LeetCode.
Insert picture description here
I don’t have much to say. The topic is as follows: First of all, I saw the topic with a dazed TAT, I don’t know where to start!
After thinking carefully:
we must traverse the array, right?
The key is how to judge whether it is an island.
We have to judge whether it is connected. For example, the current coordinate point is '1'. If the place connected to it is '1', it means that the two points are an island. By checking this as '1' We also need to judge whether the surroundings are '1'! But if after the judgment is finished, other points will also be judged to the already judged points, what should be done! ? ?
Now that the judged point has been judged, it means that there is no '1' around it. Then we can use the idea of ​​"infection" to change the judged point into '0', then when traversing the two-dimensional array, it will not conflict.
As shown in the figure below:
This is the first '1' encountered when we traverse the array,
Insert picture description here
and after we have completed the judgment, we will change it to '0', and then proceed to the next judgment:
Insert picture description here

From the above figure, we can get our idea: traverse the array, if the current node is 1, then use the depth-first search (DFS) recursively to change the point, and change the node that traverses to 1 to 0.
Then we come out, How to design an algorithm?
Recursive function (dfs) First of all, we have to think of the end condition of recursion. Naturally, when the coordinates are out of bounds, what else? When the current coordinate is '0', we will end the search from that point onwards! This is the end condition.
Next on the code:

class Solution {
    public int numIslands(char[][] grid) {
        //判空
        if(grid==null||grid.length==0){
            return 0;
        }

        int landNum=0; //岛屿个数
        //遍历数组
        for(int i=0;i<grid.length;i++){
            for(int j=0;j<grid[0].length;j++){
            if(grid[i][j]=='1'){
                landNum++; //在这里进行岛屿的增加
                dfs(grid,i,j);//深度优先搜索,遍历
            }
            }
        }
        return landNum;
    }
 //递归函数
    public void dfs(char[][] grid,int i,int j){
        //结束条件,坐标越界,或者当前位'0'
        if(i<0||j<0||i>=grid.length||j>=grid[0].length||grid[i][j]=='0'){
            return ;
        }
        
        //将当前的点变为'0' 然后对上下左右的点进行递归
        grid[i][j]='0';
        dfs(grid,i-1,j);
        dfs(grid,i+1,j);
        dfs(grid,i,j-1);
        dfs(grid,i,j+1);
    }
}

The difficulty of this question is how to judge how to form an island, and then it will not repeat the previous judgment when it traverses to other things, as well as the recursive conditions and writing!
Insert picture description hereThis is the topic of this explanation. If there is any need for improvement, please leave a message below, thank you!

Guess you like

Origin blog.csdn.net/Pzzzz_wwy/article/details/105647153