-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumIslands.h
64 lines (60 loc) · 1.88 KB
/
numIslands.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//
// Created by so_go on 2019/6/17.
//
#ifndef SRC_NUMISLANDS_H
#define SRC_NUMISLANDS_H
#include<vector>
using namespace std;
class NumIslands {
public:
void dfs(int i, int j, vector<vector<char>> &grid, vector<vector<bool>> &visited){
int nrows = grid.size(), ncols = grid[0].size();
visited[i][j] = true;
if(i + 1 < nrows and grid[i + 1][j] == '1' and not visited[i + 1][j]){
dfs(i + 1, j, grid, visited);
}
if(i - 1 >= 0 and grid[i - 1][j] == '1' and not visited[i - 1][j]){
dfs(i - 1, j, grid, visited);
}
if( j + 1 < ncols and grid[i][j + 1] == '1' and not visited[i][j + 1]){
dfs(i, j + 1, grid, visited);
}
if( j - 1 >= 0 and grid[i][j - 1] == '1' and not visited[i][j - 1]){
dfs(i, j - 1, grid, visited);
}
}
int numIslands(vector<vector<char>>& grid) {
if(grid.size() == 0){
return 0;
}
int nrows = grid.size(), ncols = grid[0].size();
int count = 0;
bool finished = true;
int row = -1, col = -1;
for(int i = 0; i < nrows * ncols; i++){
row = i / ncols;
col = i % ncols;
if(grid[row][col] == '1'){
finished = false;
break;
}
}
vector<vector<bool>> visited(nrows, vector<bool>(ncols, false));
while(not finished){
cout << row << ' ' << col << endl;
dfs(row, col, grid, visited);
count++;
finished = true;
for(int i = 0; i < nrows * ncols; i++){
row = i / ncols;
col = i % ncols;
if(grid[row][col] == '1' and not visited[row][col]){
finished = false;
break;
}
}
}
return count;
}
};
#endif //SRC_NUMISLANDS_H