zoukankan      html  css  js  c++  java
  • [LeetCode#200]Number of Islands

    Problem:

    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.

    Example 1:

    11110
    11010
    11000
    00000

    Answer: 1

    Example 2:

    11000
    11000
    00100
    00011

    Answer: 3

    Analysis:

    This problem is really really tricky, you may try to use dynamic programming to solve this problem, but it actually very hard to define. Since wheather a island is vlaid or not, it depends no only on the islands appeared before it, it also depends on the islands after it. 
    
    Wrong solution 1:
    Try to check grids around the grid to decide if a grid is a isolated is island.
    But how many isolated regions in total, how do you answer this question?
    
    Solution 1
    public int numIslands(char[][] grid) {
            if (grid == null)
                return 0;
            if (grid.length == 0 || grid[0].length == 0)
                return 0;
            int sum = (grid[0][0] == '1') ? 1 : 0;
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    if (i != 0 && j != 0) {
                        if (grid[i][j-1] == '0' && grid[i-1][j] == '0' && grid[i][j] == '1')
                            sum++;
                    } else if (i == 0 && j != 0) {
                        if (grid[i][j-1] == '0' && grid[i][j] == '1')
                            sum++;
                    } else if (i != 0 && j == 0) {
                        if (grid[i-1][j] == '0' && grid[i][j] == '1')
                            sum++;
                    } else {
                        continue;
                    }
                }
            }
            return sum;
        }
    
    
    Wrong solution 2:
    Try to use dynamic prgramming, but fail to consdier and tackle some cases.
    Invariance: If a island connected with a island in left or above direction. we don't count it as a isolated region.
    
    Error cases:
    Input:
    ["111",
    "010",
    "111"]
    Output:
    2
    Expected:
    1
    
    Fail to consdier the island appears after the current island could connect to a counted island.
        
    Solution 2:
    public int numIslands(char[][] grid) {
            if (grid == null || grid.length == 0 || grid[0].length == 0)
                return 0;
            int count = 0;
            boolean is_island[] = new boolean[grid[0].length];
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    boolean flag = false;
                    if (i != 0)
                        flag = flag || is_island[j];
                    if (j != 0)
                        flag = flag ||  is_island[j-1];
                    if (flag == false && grid[i][j] == '1') {
                        count++;
                    }
                    is_island[j] = (grid[i][j] == '1');
                }
            }
            return count;
        }
        
        
    The great solution:
    Why you spend so much efforts in thinking in tradition way.
    Since once a island connect to a island, we count them as a isolated island together. And we only care about four directions of a grid. Why do we use recursive method?
    Idea:
    Once we encounter a gird == '1', we increase count, and merge all islands in the same isolated region.
    Since the grid may directly connected with current island, we must do the merge process recursively. 
    1. count a isolated region.
            for (int i = 0; i < grid.length; i++) {
                for (int j = 0; j < grid[0].length; j++) {
                    if (grid[i][j] == '1') {
                        count++;
                        merge(grid, i, j);
                    }   
                }
            }
    
    2. merge all grids in the same isolated region.
    
    if (grid[i][j] == '1') {
        count++;
        merge(grid, i, j);
    }   
    
    private void merge(char[][] grid, int i, int j) {
            if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length)
                return;
            if (grid[i][j] == '0')  return;
            grid[i][j] = '0';
            merge(grid, i-1, j);
            merge(grid, i+1, j);
            merge(grid, i, j-1);
            merge(grid, i, j+1);
    }
    
    Skills:
    1. tackle invalid index at base case rather than at fork part. 
    if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length)
        return;
        
    2. only merge island rather than water!!! otherwise, we would merge all grids on the map. 
    if (grid[i][j] == '0')  return;

    Solution:

    public class Solution {
        public int numIslands(char[][] grid) {
            if (grid == null || grid.length == 0 || grid[0].length == 0)
                return 0;
            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') {
                        count++;
                        merge(grid, i, j);
                    }   
                }
            }
            return count;
        }
        
        private void merge(char[][] grid, int i, int j) {
            if (i < 0 || j < 0 || i >= grid.length || j >= grid[0].length)
                return;
            if (grid[i][j] == '0')  return;
            grid[i][j] = '0';
            merge(grid, i-1, j);
            merge(grid, i+1, j);
            merge(grid, i, j-1);
            merge(grid, i, j+1);
        }
    }
  • 相关阅读:
    关于如何实现NSNotificationCenter在不同的VC对象之间发送通知
    关于iOS如何实现一个单例
    C++中级-类模板
    C++中级-文件读写
    C++中级-多态
    C++中级-继承
    C++中级-friend关键字访问类中private
    C++中级-(静态成员和对象丶this指针丶常函数和对象、成员函数和对象)
    C++中级-(构造函数,拷贝构造函数,析构函数)
    C++中级-类的封装
  • 原文地址:https://www.cnblogs.com/airwindow/p/4759378.html
Copyright © 2011-2022 走看看