Number of Islands

Description

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/number-of-islands
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

——————————————————————————————

 深搜,当遇到一个没有被标记的“1”时,以此为起点,标记,继续往这个方向搜索,遇到“1”时,重复上述操作,直到此方向上没有相邻的“1”,换个方向,当当前的“1”的周围并没有相邻的“1”时,回溯到上一个“1”,换个方向,继续。。。

可以看到,其实就是一个操作,不断重复,所以很显然可以用递归的方法来做(废话,深搜不是递归还是啥。。。)

下面上代码:

class Solution {
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    public int numIslands(char[][] grid) {
        int rows = grid.length;
        if(rows == 0){
            return 0;
        }
        int cols = grid[0].length;
        int counts = 0;
        boolean[][] visited = new boolean[rows][cols];
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < cols; j++){
                if(!visited[i][j] && grid[i][j] == '1'){
                    counts++;
                    DFS(grid, visited, i, j);
                }
            }
        }
        return counts;
    }

    public static void DFS(char[][] grid, boolean[][] visited, int x, int y){
        int rows = grid.length;
        int cols = grid[0].length;
        for(int i = 0; i < 4; i++){
            int r = x + dx[i];
            int c = y + dy[i];
            if(r >= 0 && r < rows && c >= 0 && c < cols){
                if(!visited[r][c] && grid[r][c] == '1'){
                    visited[r][c] = true;
                    DFS(grid, visited, r, c);
                }
            }
        }

    }
}

 

 试了一下广搜,好像也是可以的

class Solution {
    static int[] dx = {-1, 1, 0, 0};
    static int[] dy = {0, 0, -1, 1};
    static class Node{
        int x, y;
    };
    public int numIslands(char[][] grid) {
        int rows = grid.length;
        if(rows == 0){
            return 0;
        }
        int cols = grid[0].length;
        int counts = 0;
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < cols; j++){
                if(grid[i][j] == '1'){
                    Node n = new Node();
                    n.x = i;
                    n.y = j;
                    grid[i][j] = '0';
                    BFS(n, grid);
                    counts++;
                }
            }
        }
        return counts;
    }

    public static void BFS(Node n, char[][] grid) {
        int rows = grid.length, cols = grid[0].length;
        Node[] queue = new Node[rows*cols];
        int head = 0, tail = 0;
        queue[tail++] = n;
        while(head < tail) {
            Node node = queue[head++];
            for(int i = 0; i < 4; i++) {
                int x = node.x + dx[i];
                int y = node.y + dy[i];
                if(x >= 0 && x < rows && y >= 0 && y < cols) {
                    if(grid[x][y] == '1') {
                        Node nd = new Node();
                        nd.x = x;
                        nd.y = y;
                        queue[tail++] = nd;
                        grid[x][y] = '0';
                    }
                }
            }
        }
    }
}

但是为甚么我觉得我写的广搜其实就是深搜,囧..

你可能感兴趣的:(Number of Islands)