-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCountIslands.cpp
129 lines (121 loc) · 3 KB
/
CountIslands.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
/*
GIven a 2D array of 0s and 1s.Find number of islands,the island is defined
as a group of 1s that are adjacent to each other.Code the most efficient possible solution.
Most optimized approach ->
Do BFS on each one in the binary matrix and keep track of visited element,since we are coding
for most efficient possible solution,To keep track of visited elements we'll use another matrix
just to track whether we have previously visited that element.
!Now since BFS centrally expand from the centre point,crucially it is also important to note that number of
elements can increase exponentially after every iteration in the worst case.So space will
increase exponentially after every step.
Overall the space/time complexity is O(n*m).
We could also have solve this same problem with DFS.
*/
class Solution
{
bool visited[101][101];
bool checkNeighbour(const vector<vector<int>>& binaryMatrix, int row, int col)
{
if (row >= binaryMatrix.size() || col >= binaryMatrix[0].size() || row < 0 || col < 0)
return false;
if (visited[row][col] == true || binaryMatrix[row][col] == 0)
return false;
visited[row][col] = true;
return true;
}
void bfs(const vector<vector<int>>& binaryMatrix, int row, int col)
{
queue<pair<int, int>> q;
visited[row][col] = true;
q.push({row, col});
while (!q.empty())
{
pair<int, int> front = q.front();
q.pop();
int x = front.first;
int y = front.second;
// checking its neighbours
// (x,y) -> (x+1,y),(x-1,y),(x,y+1),(x,y-1)
if (checkNeighbour(binaryMatrix, x + 1, y))
{
q.push({x + 1, y});
}
if (checkNeighbour(binaryMatrix, x - 1, y))
{
q.push({x - 1, y});
}
if (checkNeighbour(binaryMatrix, x, y + 1))
{
q.push({x, y + 1});
}
if (checkNeighbour(binaryMatrix, x, y - 1))
{
q.push({x, y - 1});
}
}
}
void initializeVisitedSet(const vector<vector<int>>& binaryMatrix)
{
for (int i = 0; i < binaryMatrix.size(); i++)
{
for (int j = 0; j < binaryMatrix[i].size(); j++)
{
visited[i][j] = false;
}
}
}
public:
Solution()
{
}
int getNumberOfIslands( const vector<vector<int>>& binaryMatrix )
{
// your code goes here
int num_islands = 0;
if (binaryMatrix.size() == 0)
{
return 0;
}
initializeVisitedSet(binaryMatrix);
for (int i = 0; i < binaryMatrix.size(); i++)
{
for (int j = 0; j < binaryMatrix[i].size(); j++)
{
if (visited[i][j] || binaryMatrix[i][j] == 0)
{
continue;
}
bfs(binaryMatrix, i, j);
num_islands += 1;
}
}
return num_islands;
}
};
int main()
{
int test_cases;
cin >> test_cases;
Solution s;
while(test_cases--)
{
int row,col;
cin >> row >> col;
vector<vector<int> > binary_matrix;
for(int i=0;i<row;i++)
{
vector<int> column;
for(int j=0;j<col;j++)
{
int temp;
cin >> temp;
column.push_back(temp);
}
binary_matrix.push_back(column);
}
cout <<"Number of islands: " << s.getNumberOfIslands(binary_matrix) << endl;
}
}