Leetcode problem solution 200-number of islands

Problem Description

Given 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 connecting adjacent land in horizontal and/or vertical directions.

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

Example 1:

输入:grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
输出:1

Example 2:

输入:grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
输出:3

Problem-solving ideas (depth first search DFS):

The number of islands here is actually the number of connected graphs in the graph, and the number of connected graphs in the graph is actually a loop in the outer layer of the depth-first search. Then every time the search meets the conditions, the search will be found The entire connected graph (that is, an island), we mark it all, and finally we can calculate the total number of connected graphs in the graph.
We can think of a two-dimensional grid as an undirected graph, with edges connected between vertically or horizontally adjacent ones.
To find the number of islands, we can scan the entire two-dimensional grid. If a position is 1, then use it as the starting node to start the depth-first search. During the depth-first search, every 1 found will be re-marked as 0.
In the end, the number of islands is the number of times we perform a depth-first search.

Implementation code

class Solution {
    
    
    private int[][] visited;        //标记数组,标记有没有走过
	private int row;
    private int col;
    public void dfs(int i,int j,char [][]grid){
    
    
        //如果没有走到上边的边界
        //并且上边仍然是陆地,且没有走过
        if(i-1>=0 && (grid[i-1][j]=='1' && visited[i-1][j]==0)){
    
    
            visited[i-1][j]=1;
            dfs(i-1,j,grid);
        }
        //向下走
        if(i+1<row && (grid[i+1][j]=='1' && visited[i+1][j]==0)){
    
    
            visited[i+1][j]=1;
            dfs(i+1,j,grid);
        }
        //向左走
        if(j-1>=0 && (grid[i][j-1]=='1' && visited[i][j-1]==0)){
    
    
            visited[i][j-1]=1;
            dfs(i,j-1,grid);
        }
        //向右走
        if(j+1<col && (grid[i][j+1]=='1' && visited[i][j+1]==0)){
    
    
            visited[i][j+1]=1;
            dfs(i,j+1,grid);
        }
    }
    public int numIslands(char[][] grid) {
    
    
		row=grid.length;
        col=grid[0].length;
        int count=0;            //统计岛屿数量
        visited=new int[row][col];
        //在外层用一个for循环,对每个元素尝试以它为起点进行遍历
        //每次遍历的时候就可以遍历一个连通图(即一个岛屿)
        for(int i=0;i<row;i++){
    
    
            for(int j=0;j<col;j++){
    
    
                if(grid[i][j]=='1' && visited[i][j]==0){
    
    
                    visited[i][j]=1;
                    dfs(i,j,grid);
                    count++;
                }
            }
        }
        return count;
    }
}

Guess you like

Origin blog.csdn.net/qq_39736597/article/details/114032262