###### Lintcode解题笔记 - 岛屿的个数

0代表海，1代表岛，如果两个1相邻，那么这两个1属于同一个岛。我们只考虑上下左右为相邻。

[
[1, 1, 0, 0, 0],
[0, 1, 0, 0, 1],
[0, 0, 0, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 1]
]

class Solution {
public:
/**
* @param grid a boolean 2D matrix
* @return an integer
*/
int numIslands(vector<vector<bool>>& grid) {
int row = grid.size();
if(row < 1)
return 0;
int column = grid[0].size();
vector<int> tmp(column, 0);
vector<vector<int>> visited(row, tmp);
//int visited[row][column] = 0;
//vector<vector<bool>> visited = grid;
int result = 0;
for(int i = 0; i < row; i++){
for(int j = 0; j < column; j++ ){
if(grid[i][j] == true && visited[i][j] != 1){
floodRush(i,j, visited, grid);
result++;
}

}
}
return result;

}

void floodRush(int row, int column, vector<vector<int>> & visited, vector<vector<bool>> grid){
if(row < 0 || column < 0 || row >= grid.size() || column >= grid[0].size() ){
return;
}
if(visited[row][column] == 1)
return;
if(grid[row][column] == true){
visited[row][column] = 1;
floodRush(row-1, column, visited, grid);
floodRush(row+1, column, visited, grid);
floodRush(row, column-1, visited, grid);
floodRush(row, column+1, visited, grid);
}else{
return;
}

}

};

#### leetcode解题之200. Number of Islands Java版（岛屿的数量）

2017-04-05 22:07:17

#### LintCode 岛屿的个数

2015-08-22 23:06:35

#### 51Nod 1276 岛屿的数量(好题)

2016-12-13 12:14:06

#### 岛屿数量

2018-03-13 22:49:47

#### LintCode:岛屿的个数

2017-04-04 17:01:53

#### 岛屿的个数

2017-06-08 20:35:31

#### LintCode 433. 岛屿的个数

2016-03-15 17:29:36

#### 岛屿的个数（LintCode）

2015-10-25 18:51:56

#### Google 2016 面试题5 | 岛屿计数2

2016-03-24 17:24:11

#### 华为OJ-岛屿个数问题

2016-09-09 00:15:29

## 不良信息举报

Lintcode解题笔记 - 岛屿的个数