-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber-of-Islands.cpp
46 lines (42 loc) · 1.02 KB
/
Number-of-Islands.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
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void DFS(vector<vector<char>> &grid, int r, int c, int n, int m)
{
grid[r][c] = '0';
if (r - 1 >= 0 && grid[r - 1][c] == '1')
DFS(grid, r - 1, c, n, m);
if (r + 1 < n && grid[r + 1][c] == '1')
DFS(grid, r + 1, c, n, m);
if (c - 1 >= 0 && grid[r][c - 1] == '1')
DFS(grid, r, c - 1, n, m);
if (c + 1 < m && grid[r][c + 1] == '1')
DFS(grid, r, c + 1, n, m);
}
int numIslands(vector<vector<char>> &grid)
{
int n = grid.size(), m = grid[0].size(), count = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (grid[i][j] == '1')
{
count++;
DFS(grid, i, j, n, m);
}
}
}
return count;
}
};
int main()
{
Solution st;
vector<vector<char>> grid = {{'1', '1', '1', '1', '0'}, {'1', '1', '0', '1', '0'}, {'1', '1', '0', '0', '0'}, {'0', '0', '1', '0', '1'}};
int ans = st.numIslands(grid);
cout << ans << endl;
return 0;
}