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.

Example 1:

Input:

11110
11010
11000
00000

Output:

1

Example 2:

Input:

11000
11000
00100
00011

Output:

3

分析

题目的意思是:1代表陆地,0代表水,求矩阵表示的地图中陆地的数量,1上下左右相邻代表一个陆地。

  • 这个是经典的深度优先搜索的问题,如果某位置上为1,就进行深度优先搜索,把搜索到1的位置置0,这样就避免了重复遍历。然后遍历几次就有多少个分隔的陆地,直接返回就行了。

代码

class Solution {
private:
int d[4][2]={
{-1, 0},{0, -1},{0, 1},{1, 0}
};
public:
int numIslands(vector<vector<char>>& grid) {
if(grid.size()==0){
return 0;
}
int rows=grid.size();
int cols=grid[0].size();
int count=0;
for(int i=0;i<rows;i++){
for(int j=0;j<cols;j++){
if(grid[i][j]=='1'){
dfs(grid,i,j);
count++;
}
}
}
return count;
}
void dfs(vector<vector<char>>& grid,int i,int j){
if(i<0||i>=grid.size()||j<0||j>=grid[0].size()){
return;
}
if(grid[i][j]=='0'){
return ;
}
grid[i][j]='0';
for(int k=0;k<4;k++){
dfs(grid,i+d[k][0],j+d[k][1]);
}
}
};