-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathNumberofIslands.cpp
50 lines (44 loc) · 1.34 KB
/
NumberofIslands.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
// 200. Leetcode {MEDIUM} [BFS,DFS]
#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
void DFS(vector<vector<char>> &grid, int i, int j)
{
// boundary checking
if (i < 0 || i >= grid.size() || j < 0 || j >= grid[0].size())
return;
// return if current position is of water or is already visited
if (grid[i][j] == '2' || grid[i][j] == '0')
return;
// mark the current as visited
grid[i][j] = '2';
// do DFS in all 4 directions
DFS(grid, i + 1, j);
DFS(grid, i, j - 1);
DFS(grid, i - 1, j);
DFS(grid, i, j + 1);
}
int numIslands(vector<vector<char>> &grid)
{
// We can treat the matrix grid as a grid. Each Island is a
// connected component. The task is to find no. of disconnectedd components
// in the graph.
int islands = 0;
// We make each 1 as 2 in when it is visited
for (int i = 0; i < grid.size(); i++)
{
for (int j = 0; j < grid[0].size(); j++)
{
// do DFS in case has not been visited and there is land
if (grid[i][j] == '1')
{
DFS(grid, i, j);
++islands;
}
}
}
return islands;
}
};