-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber of Islands__dfs.cpp
73 lines (62 loc) · 1.73 KB
/
Number of Islands__dfs.cpp
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
65
66
67
68
69
70
71
72
73
/*
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.
*/
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
void dfs(vector<vector<char>> &grid, int x, int y, int w, int h){
if (x < 0 || x >= h || y < 0 || y >= w ) return;
if (grid[x][y] == '0') return;
else
{
grid[x][y] = '0';
dfs(grid, x - 1, y, w, h);
dfs(grid, x + 1, y, w, h);
dfs(grid, x, y - 1, w, h);
dfs(grid, x, y + 1, w, h);
}
}
int numIslands(vector<vector<char>> &grid) {
int w = 0, h = 0;
if (grid.empty()) return 0;
w = grid[0].size();
h = grid.size();
int ans = 0;
for (int i = 0; i < h; i++){
for (int j = 0; j < w; j++){
if (grid[i][j] == '1'){
ans++;
dfs(grid,i, j, w, h);
}
}
}
return ans;
}
};
int main()
{
Solution mySolu;
vector<vector<char>> Grid;//((1,1,0,0,0),(1,1,0,0,0),(0,0,1,0,0),(0,0,0,1,1));
char init0[] = { '1', '1', '0', '0', '0' };
size_t s_count = sizeof(init0) / sizeof(char);
vector<char>temp0(init0, init0+s_count);
char init1[] = { '1', '1', '0', '0', '0' };
vector<char>temp1(init1, init1+ s_count);
char init2[] = { '0', '0', '1', '0', '0' };
vector<char>temp2(init2, init2 + s_count);
char init3[] = { '0', '0', '0', '1', '1' };
vector<char>temp3(init3, init3 + s_count);
char init4[] = { '1', '1', '0', '0', '0' };
vector<char>temp4(init4, init4 + s_count);
/*
Grid.push_back(temp0);
Grid.push_back(temp1);
Grid.push_back(temp2);
Grid.push_back(temp3);*/
//Grid.push_back(temp4);
cout << mySolu.numIslands( Grid );
}