200. Number of Islands

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:
这道题可以使用深搜来解决。为方便,下面代码中使用栈替代递归来完成深搜。

Source Code(C++):

#include <iostream>
#include <vector>
#include <stack>
using namespace std;

class Solution {
public: 
    int numIslands(vector<vector<char> >& grid) {
        if (grid.empty())
            return 0;
        int row=grid.size();
        int col=grid[0].size();
        vector<vector<int> > color(row, vector<int>(col, 0));  //标记是否已经走过此位置
        int roomNum=0;
        for(int i=0; i<row; i++)
            for (int j=0; j<col; j++) {
                if (!color[i][j] && grid[i][j]=='1') {  //如果为陆地且没有走过,则从此位置开始深搜
                    roomNum++;
                    //使用栈替代递归来完成深搜
                    struct Location {int r, c; Location(int rr, int cc):r(rr),c(cc){} };
                    stack<Location> stk;  
                    stk.push(Location(i, j));
                    while(!stk.empty()) {
                        Location lt = stk.top();
                        int i=lt.r; int j=lt.c;
                        if (color[i][j]) {
                            stk.pop();
                        } 
                        else {
                            color[i][j] = roomNum;
                            if (i>0 && grid[i-1][j]=='1') stk.push(Location(i-1, j)); //向上走
                            if (i<(row-1) && grid[i+1][j]=='1') stk.push(Location(i+1, j)); //向下走
                            if (j>0 && grid[i][j-1]=='1') stk.push(Location(i, j-1)); //向左走
                            if (j<(col-1) && grid[i][j+1]=='1') stk.push(Location(i, j+1)); //向右走
                        }
                    }
                }
            }
        return roomNum;
    }
};

int main() {
    Solution sol;
    vector<vector<char> > v(4);
    v[0].push_back('1');v[0].push_back('1');v[0].push_back('0');v[0].push_back('0');v[0].push_back('0');
    v[1].push_back('1');v[1].push_back('1');v[1].push_back('0');v[1].push_back('0');v[1].push_back('0');
    v[2].push_back('0');v[2].push_back('0');v[2].push_back('1');v[2].push_back('0');v[2].push_back('0');
    v[3].push_back('0');v[3].push_back('0');v[3].push_back('0');v[3].push_back('1');v[3].push_back('1');
    cout << sol.numIslands(v) << endl;
    vector<vector<char> > v1(1);
    v1[0].push_back('1');v1[0].push_back('1');v1[0].push_back('0');v1[0].push_back('0');v1[0].push_back('0');
    cout << sol.numIslands(v1) << endl;
    vector<vector<char> > v2(4);
    v2[0].push_back('1');
    v2[1].push_back('1');
    v2[2].push_back('0');
    v2[3].push_back('0');
    cout << sol.numIslands(v2) << endl;
    vector<vector<char> > v3;
    cout << sol.numIslands(v3) << endl;
    return 0;
}

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