The number of 200 islands: Leetcode.

Title:
Given the a '1' (land) and '0' (water) consisting of a two-dimensional grid, the number of islands is calculated. An island surrounded by water, and it is through the horizontal or vertical direction is connected to each adjacent land. You can assume that the water surrounding the four sides of the grid are.
Example 1:
Input:
11110
11010
11000
00000

Output: 1
Example 2:
Input:
11000
11000
00100
00011

Output: 3

DFS learn
codes from English leetcode

public int numIslands(char[][] grid) {
    int count=0;
    for(int i=0;i<grid.length;i++)
        for(int j=0;j<grid[0].length;j++){
            if(grid[i][j]=='1'){
                dfsFill(grid,i,j);
                count++;//深度搜索完一次加一次
            }
        }
    return count;
}
private void dfsFill(char[][] grid,int i, int j){
    if(i>=0 && j>=0 && i<grid.length && j<grid[0].length&&grid[i][j]=='1'){
        grid[i][j]='0';
        dfsFill(grid, i + 1, j);
        dfsFill(grid, i - 1, j);
        dfsFill(grid, i, j + 1);
        dfsFill(grid, i, j - 1);
    }
}

BFS learn
codes from English leetcode

public int numIslands(char[][] grid) {
    int count=0;
    for(int i=0;i<grid.length;i++)
        for(int j=0;j<grid[0].length;j++){
            if(grid[i][j]=='1'){
                bfsFill(grid,i,j);
                count++;
            }
        }
    return count;
}
private void bfsFill(char[][] grid,int x, int y){
    grid[x][y]='0';
    int n = grid.length;
    int m = grid[0].length;
    LinkedList<Integer> queue = new LinkedList<Integer>();  
    int code = x*m+y;  
    queue.offer(code);  
    while(!queue.isEmpty())  
    {  
        code = queue.poll();  
        int i = code/m;  
        int j = code%m;  
        if(i>0 && grid[i-1][j]=='1')    //search upward and mark adjacent '1's as '0'.
        {  
            queue.offer((i-1)*m+j);  
            grid[i-1][j]='0';  
        }  
        if(i<n-1 && grid[i+1][j]=='1')  //down
        {  
            queue.offer((i+1)*m+j);  
            grid[i+1][j]='0';  
        }  
        if(j>0 && grid[i][j-1]=='1')  //left
        {  
            queue.offer(i*m+j-1);  
            grid[i][j-1]='0';  
        }  
        if(j<m-1 && grid[i][j+1]=='1')  //right
        {  
            queue.offer(i*m+j+1);  
            grid[i][j+1]='0';  
        }
    } 
}

4.5 Review

class Solution {
    public int numIslands(char[][] grid) {
        int count=0;
        
        for(int i=0;i<grid.length;i++){//错误:数组~无();二维数组也是数组
            for(int j=0;j<grid[0].length;j++){ //错误:grid[0]未加.length
                if(grid[i][j]=='1'){
                    setzero(grid,i,j);
                    count++;
                }
            }
        }
        
        return count;
    }
    
        
    public void setzero (char[][]grid,int i,int j){

        
        grid[i][j]='0';
        if(i+1<grid.length&&grid[i+1][j]=='1') setzero(grid,i+1,j);//错误:没有加上i+1<grid.length
        
        if(i-1>=0&&grid[i-1][j]=='1') setzero(grid,i-1,j); //错误:i-1>0
        
        if(j+1<grid[0].length&&grid[i][j+1]=='1') setzero(grid,i,j+1);
        
        if(j-1>=0&&grid[i][j-1]=='1') setzero(grid,i,j-1);
    }
}

Guess you like

Origin blog.csdn.net/Fishandbearspaw/article/details/88999850