-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
count_islands.cpp
91 lines (84 loc) · 2.27 KB
/
count_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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* In a given 2D grid, 1 represents land and 0 represents water body.
* We need to count number of islands.
* An island is surrounded by connecting neighbouring lands vertically
* or horizontally.
* Assume: That everything around the grid is water.
*
* Approach: We can solve this problem with Depth/Breadth first search.
*
* With Depth first search:
* Consider a land body, and start a depth first search, and mark all the
* connected land bodies to it as water bodies (i.e. mark all connected 1's
* as 0 to mark them visited)
* In this case, number of islands for which DFS was started is the total
* count of islands.
* Similar idea could be used for BFS.
*
* Example:
*
* 1 1 0 0
* 1 0 1 0
* 1 0 0 1
*
* There are 3 islands in above grid.
*/
#include <iostream>
#include <vector>
void depth_first_search(std::vector<std::vector<int> >& grid, int i, int j)
{
int m = grid.size();
int n = grid[0].size();
if (i < 0 || i >= m || j < 0 || j >= n) {
return;
}
grid[i][j] = 0;
if (i + 1 < m && grid[i + 1][j] == 1) {
depth_first_search(grid, i + 1, j);
}
if (i - 1 >= 0 && grid[i - 1][j] == 1) {
depth_first_search(grid, i - 1, j);
}
if (j + 1 < n && grid[i][j + 1] == 1) {
depth_first_search(grid, i, j + 1);
}
if (j - 1 >= 0 && grid[i][j - 1] == 1) {
depth_first_search(grid, i, j - 1);
}
}
int number_of_islands(std::vector<std::vector<int>>& grid)
{
int m = grid.size();
if (m == 0) {
return 0;
}
int n = grid[0].size();
int total_islands = 0;
for ( int i = 0; i < m; ++i) {
for ( int j = 0; j < n; ++j) {
if (grid[i][j] == 1) {
++total_islands;
depth_first_search(grid, i, j);
}
}
}
return total_islands;
}
int main()
{
std::vector<std::vector<int>> grid = {
{1, 1, 0, 0, 0},
{1, 1, 0, 0, 0},
{0, 0, 1, 0, 0},
{0, 0, 0, 1, 1}
};
for (int i = 0; i < grid.size(); ++i) {
for (int j = 0; j < grid[0].size(); ++j) {
std::cout << grid[i][j] << " ";
}
std::cout << std::endl;
}
std::cout << "Total number of islands in the grid is :"
<< number_of_islands(grid) << std::endl;
return 0;
}