forked from anshuman8800/Interivew-Questions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountIslands.cpp
More file actions
53 lines (48 loc) · 1.23 KB
/
countIslands.cpp
File metadata and controls
53 lines (48 loc) · 1.23 KB
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
// Given an m x n 2D binary grid grid which represents a map of '1's (land) and '0's (water), return the number of islands.
#include <bits/stdc++.h>
using namespace std;
void dfs(vector<vector<int>> &grid, vector<vector<bool>> &vis, int i, int j)
{
if (vis[i][j] || !grid[i][j])
return;
vis[i][j] = 1;
if (i - 1 >= 0)
dfs(grid, vis, i - 1, j);
if (i + 1 < grid.size())
dfs(grid, vis, i + 1, j);
if (j - 1 >= 0)
dfs(grid, vis, i, j - 1);
if (j + 1 < grid[0].size())
dfs(grid, vis, i, j + 1);
}
int countIslands(vector<vector<int>> &grid)
{
int n = grid.size(), m = grid[0].size(), count = 0;
vector<vector<bool>> vis(n, vector<bool>(m, 0));
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
if (!vis[i][j] && grid[i][j])
{
dfs(grid, vis, i, j);
++count;
}
}
}
return count;
}
int main()
{
int n, m;
cin >> n >> m;
vector<vector<int>> grid(n, vector<int>(m));
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < m; ++j)
{
cin >> grid[i][j];
}
}
cout << "Number of islands : " << countIslands(grid) << endl;
}