-
Notifications
You must be signed in to change notification settings - Fork 5
/
LC_200_NumOfIslands.cpp
62 lines (56 loc) · 1.51 KB
/
LC_200_NumOfIslands.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
/*
200. Number of Islands
https://leetcode.com/problems/number-of-islands/
*/
class Solution {
public:
vector<pair<int,int>> dirs = {{1,0}, {-1,0}, {0,1}, {0, -1}};
int m, n;
int numIslands(vector<vector<char>>& grid) {
m = grid.size();
n = grid[0].size();
int cnt_islands=0;
for(int i=0; i<m; i++)
{
for(int j=0; j<n;j++)
{
if(grid[i][j] == '1')
{
cnt_islands++;
// bfs_helper(i, j, grid);
dfs_helper(i, j, grid);
}
}
}
return cnt_islands;
}
void dfs_helper(int x, int y, vector<vector<char>>& grid)
{
for(auto [dx, dy] : dirs)
{
dx = x + dx;
dy = y + dy;
if(dx<0 or dx>=m or dy<0 or dy>=n or grid[dx][dy] != '1') continue;
grid[dx][dy] = '2';
dfs_helper(dx, dy, grid);
}
}
void bfs_helper(int i, int j, vector<vector<char>>& grid)
{
grid[i][j] = '2';
queue<pair<int,int>> q; q.push({i,j});
while(!q.empty())
{
auto [x, y] = q.front();q.pop() ;
for(auto [dx, dy] : dirs)
{
dx = x + dx;
dy = y + dy;
if(dx<0 or dx>=m or dy<0 or dy>=n or grid[dx][dy] != '1') continue;
grid[dx][dy] = '2';
q.push({dx,dy});
}
}
return;
}
};