forked from MaskRay/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
number-of-islands.cc
34 lines (33 loc) · 923 Bytes
/
number-of-islands.cc
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
// Number of Islands
#define FOR(i, a, b) for (decltype(b) i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
class Solution {
public:
int numIslands(vector<vector<char>> &a) {
if (a.empty()) return 0;
stack<pair<int, int>> s;
size_t m = a.size(), n = a[0].size();
int num = 0;
REP(i, m)
REP(j, n)
if (a[i][j] == '1') {
num++;
stack<pair<int, int>> s;
s.push(make_pair(i, j));
a[i][j] = 'x';
while (! s.empty()) {
int x = s.top().first, y = s.top().second;
s.pop();
REP(d, 4) {
int xx = x+(int[]){-1,0,1,0}[d],
yy = y+(int[]){0,1,0,-1}[d];
if (size_t(xx) < m && size_t(yy) < n && a[xx][yy] == '1') {
s.push(make_pair(xx, yy));
a[xx][yy] = 'x';
}
}
}
}
return num;
}
};