200. Number of Islands

Total Accepted: 48411  Total Submissions: 171609  Difficulty: Medium

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

Credits:
Special thanks to @mithmatt for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Show Tags
Show Similar Problems

分析:

这是网上流传最广的深度优先解法,确实漂亮,通俗易理解。

class Solution {
public:
    int numIslands(vector<vector<char>>& grid) {
        int result=0;
        if(grid.empty() || grid[0].empty())
            return result;
        int rows=grid.size();
        int cols=grid[0].size();
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<cols;j++)
            {
                if(grid[i][j]=='1')//如果是岛屿,那么此处一定有一座岛屿。将和他连在一起的岛屿全部置零
                {
                    dfs(grid,i,j,rows,cols);//深搜,将该位置(i,j)四周的1都置0
                    result++;//发现一座岛屿(很多个1形成,dfs后该岛置零),并且该岛屿被全部置零(不影响另外独立的岛屿判断)
                }
            }
        }
        return result;
    }
    void dfs(vector<vector<char>>& grid,int i,int j,int rows,int cols)
    {
        grid[i][j]='0';
        if(i > 0 && grid[i-1][j] == '1')//上边置0
            dfs(grid,i-1,j,rows,cols);
        if(i < rows-1 && grid[i+1][j] == '1')//下边同理   
            dfs(grid,i+1,j,rows,cols);
        if(j > 0 && grid[i][j-1] == '1')//左边   
            dfs(grid,i,j-1,rows,cols);    
        if(j < cols-1 && grid[i][j+1] == '1')//右边   
            dfs(grid,i,j+1,rows,cols);    
    }
};



学习别人的并查集解决问题,漂亮:

class Solution {
public:
    int find(vector<int> & parents, int index) {//寻找index位置的最高祖先(也是一个位置)
        if(parents[index]==-1)
            return index;
        return find(parents, parents[index]);
    }
    void merge(vector<int> & parents, int a, int b){//合并
        int parent_a = find(parents, a);
        int parent_b = find(parents, b);
        if(parent_a!=parent_b){
            parents[parent_b]=parent_a;//set the new 's parent to be old one.
            total--;
        }
    }
    int numIslands(vector<vector<char>>& grid) {
        int rows = grid.size();
        if (rows==0) return 0;
        int cols = grid[0].size();
        vector<int>parents(rows*cols,-1);
        total=rows*cols; //随后减少
        int waters=0;
        for(int i=0;i<rows;i++){
            for(int j=0;j<cols;j++){
                if(grid[i][j]=='0') 
                    waters++;
                else{
                    int index = i*cols+j;
                    //我们只需要考虑已经访问过的,并且合并他们
                    //只有相邻的两个需要合并,并且他们均为1
                    //并且总是往上边和右边合并
                    //如果该岛屿是独立的岛屿,他将会被合并成为一个独立的祖先
                    if(i>0 && grid[i-1][j]=='1')
                        merge(parents, (i-1)*cols+j, index);
                    if(j>0 && grid[i][j-1]=='1')
                        merge(parents, i*cols+j-1, index);
                }
            }
        }
        return total-waters;
    }
private:
    int total;
};




注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/51636977

原作者博客:http://blog.csdn.net/ebowtang

本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895

你可能感兴趣的:(LeetCode,C++,算法,搜索)