Leetcode topic number 200. Islands (BFS + DFS + disjoint-set - Medium)

Subject description:

Given a two-dimensional grid by a '1' (land) and '0' (water) composition, 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

Ideas Analysis: problem and solution from the example of FIG: liweiwei1419

Description: The algorithm described below, in addition to the disjoint-set, DFS and BFS are all very basic algorithm content, is also very easy to understand, the wording is relatively fixed, the reader needs to write, find and record their own problems, I also write even several times during the writing of this solution to a problem, only to find out their own problems.

This question is a classical algorithm can be used to solve, and that is Flood fill, the following definition from Wikipedia: Flood fill entry.

Flood fill algorithm is to extract the number of points from a communication area from other areas adjacent to the area (or dyed different colors, respectively) of the classical algorithm. The idea is similar to flooding because of diffusion from one area to reach all areas of the name. In the GNU Go and clearance, Flood Fill algorithm is used to calculate area needs to be cleared.

"Flood" I checked, as a verb is "drown; full" means, as the term is "flood" means. Here we briefly explain the algorithm:

Extracted from an area several points in communication with other adjacent areas distinguish
spread from one point to find the point with which it communicates, this is not rocket algorithm, in fact, starting from a point, to conduct a "depth-first traversal" or "breadth-first traversal," through the "depth-first traversal" or "breadth-first traversal" discovery area of an attached, for this question, it is to be from a "land" box to begin a "depth-first traversal" or "breadth-first traversal," all the grid connected to it are on the mark, considered to found a "islands."

Note: do here "mark" means, through the "depth-first traversal" or "breadth-first traversal" operation, I found a new grid, with the starting point of the grid is connected, we considered a "mark" over , you can also say "been visited."

So every time a "depth-first traversal" or "breadth-first traversal" condition is:

1, this is a land grid 1, if the water is 0 can never talk about "islands";

2, the grid can not be found before the "islands" during the execution of a "depth-first traversal" or "breadth-first traversal" operations, are marked grid.

A thought: DFS

Code:

package com.company;

/**
 * @author yaoshw
 */
public class Main {

    public static void main(String[] args) {
        char[][] grid2 = {
                {'1', '1', '0', '0', '0'},
                {'1', '1', '0', '0', '0'},
                {'0', '0', '1', '0', '0'},
                {'0', '0', '0', '1', '1'}};
        System.out.println(numIslands(grid2));

    }

    public static int numIslands(char[][] grid) {

        //grid的行
        int row = grid.length;
        if the coordinate point is // tag is accessed grid
        // grid column
        }
            return 0;
        IF (Row == 0) {
        int col = grid[0].length;
        Boolean [] [] = new new Boolean visited [Row] [COL]; 
        // current grid represents the four directions, the -> Right -> next -> Left 
        int [] [] directions = new int [] [] { 
                {-1, 0}, {0, -1}, {1, 0}, {0, 1} 
        }; 
        number of islands // find the accumulated 
        int COUNT = 0; 
        for (int I = 0; I <Row ; I ++) { 
            for (int J = 0; J <COL; J ++) { 
                IF (Grid [I] [J] == '. 1' && visited [I] [J]) {! 
                    COUNT ++; 
                    // from the current position start depth-first traversal (DFS) 
                    DFS (I, J, Directions, visited, Row, COL, Grid); 
                } 
            } 
        } 
        return COUNT; 
    } 

    // DFS
    static void DFS Private (int I, J int, int [] [] Directions, Boolean [] [] visited, Row int, int COL, char [] [] Grid) { 

        // mark the current node has been accessed 
        visited [i ] [J] = to true; 

        // where k is from 0 to 3, from one point to the next represents a possible grid, are the coordinates 
        for (int k = 0; k <4; k ++) { 
            next to a // walking grid x coordinate 
            int newX = i + directions [k ] [0]; 
            the next one to go // Y coordinate grid 
            int + Directions newY J = [K] [. 1]; 

            // do not go out of range, and is not accessed, and land or 
            IF (inArea (newX, newY, Row, COL) &&! visited [newX] [newY] && Grid [newX] [newY] == '. 1') { 
                DFS (newX, newY, Directions , visited, Row, COL, Grid); 
            } 
        } 
    } 

    // if cross-border
    private static boolean inArea(int newX, int newY, int row, int col) {
        return newX >= 0 && newX < row && newY >= 0 && newY < col;
    }
}

Thinking two: BFS

Thinking three: disjoint-set

time complexity:

Space complexity:

Guess you like

Origin www.cnblogs.com/ysw-go/p/11887799.html