-
Notifications
You must be signed in to change notification settings - Fork 0
/
number_of_islands.cpp
190 lines (170 loc) · 5.14 KB
/
number_of_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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// https://leetcode.com/problems/number-of-islands/
// May 03, 2016
#include <iostream>
#include <string>
#include <vector>
#include <queue>
using namespace std;
void print_grid(vector<vector<char> >& grid)
{
for(unsigned int row=0; row != grid.size(); ++row)
{
for (unsigned int col=0; col != grid[row].size(); ++col)
{
cout << grid[row][col];
}
// just for testing
// grid[row][grid[row].size()-1] = '1';
cout << endl;
}
}
class Solution {
public:
int numIslands(vector<vector<char> >& grid)
{
grid_ = grid;
nrow_ = grid_.size();
// Handle empty grid
if (nrow_ == 0)
{
return 0;
}
ncol_ = grid_[0].size();
// For testing print grid_
// print_grid(grid_);
initialize_visit_matrix(grid_);
// Now compute number of connected components using BFS traversal
int n_connected_components = 0;
for(unsigned int row=0; row != grid.size(); ++row)
{
for (unsigned int col=0; col != grid[row].size(); ++col)
{
if ((grid_[row][col] == '1') && !visitMatrix_[row][col])
{
bool flag = bfs_traversal(row, col);
if (flag)
{
++n_connected_components;
}
}
}
}
return n_connected_components;
}
private:
void initialize_visit_matrix(vector<vector<char> >& grid)
{
for(unsigned int row=0; row != grid.size(); ++row)
{
vector<bool> vecBool;
vecBool.reserve(grid[row].size());
for (unsigned int col=0; col != grid[row].size(); ++col)
{
vecBool.push_back(false);
}
visitMatrix_.push_back(vecBool);
}
}
// return false if (a) grid[row,col] is already visited
// (b) grid[row,col] == 0 i.e. its in water
bool bfs_traversal(unsigned int row, unsigned int col)
{
if (visitMatrix_[row][col] || grid_[row][col] != '1')
{
return false;
}
queue<pair<unsigned int, unsigned int> > Q;
Q.push(make_pair(row, col));
visitMatrix_[row][col] = true;
while (!Q.empty())
{
pair<unsigned int, unsigned int> pos_coordinate = Q.front();
Q.pop();
// Push the adjacent land coordinates horizontally or vertically which are non-visited
vector<pair<unsigned int, unsigned int> > adjacent_coordinates = get_adjacent_land_coordinates(pos_coordinate.first, pos_coordinate.second);
for(vector<pair<unsigned int, unsigned int> >::iterator it = adjacent_coordinates.begin(); it != adjacent_coordinates.end(); ++it)
{
unsigned int adjacent_row = (*it).first;
unsigned int adjacent_col = (*it).second;
if (!visitMatrix_[adjacent_row][adjacent_col])
{
visitMatrix_[adjacent_row][adjacent_col] = true;
Q.push(make_pair(adjacent_row, adjacent_col));
}
}
}
return true;
}
vector<pair<unsigned int, unsigned int> > get_adjacent_land_coordinates(unsigned int row, unsigned int col)
{
vector<pair<unsigned int, unsigned int> > adjacent_coordinates;
// checking vertically adjacent coordinates
if (row > 0)
{
if (grid_[row-1][col] == '1')
{
adjacent_coordinates.push_back(make_pair(row-1, col));
}
}
if (row < (nrow_-1))
{
if (grid_[row+1][col] == '1')
{
adjacent_coordinates.push_back(make_pair(row+1, col));
}
}
// checking horizontally adjacent coordinates
if (col > 0)
{
if (grid_[row][col-1] == '1')
{
adjacent_coordinates.push_back(make_pair(row, col-1));
}
}
if (col < (ncol_-1))
{
if (grid_[row][col+1] == '1')
{
adjacent_coordinates.push_back(make_pair(row, col+1));
}
}
return adjacent_coordinates;
}
private:
vector<vector<bool> > visitMatrix_;
vector<vector<char> > grid_;
unsigned int nrow_;
unsigned int ncol_;
};
// For testing reference
void modify_grid(vector<vector<char> >& grid)
{
for(unsigned int row=0; row != grid.size(); ++row)
{
grid[row][grid[row].size()-1] = '1';
}
}
int main(int argc, char* argv[])
{
vector<vector<char> > grid;
int i = 0;
string str;
while (cin >> str)
{
++i;
vector<char> vecChar;
vecChar.reserve(str.size());
for (unsigned int j=0; j != str.size(); ++j)
{
vecChar.push_back(str[j]);
}
grid.push_back(vecChar);
// cout << "i: " << str << endl;
}
// modify_grid(grid);
// print_grid(grid);
Solution sln;
int num_islands = sln.numIslands(grid);
cout << num_islands << endl;
return 0;
}