forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAC_bfs_n.cpp
65 lines (52 loc) · 1.62 KB
/
AC_bfs_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_bfs_n.cpp
* Create Date: 2015-04-08 19:30:34
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
class Solution {
public:
int numIslands(vector<vector<char>> &grid) {
if (grid.empty() || grid[0].empty())
return 0;
queue<int> q;
int ret = 0;
int n = grid.size(), m = grid[0].size();
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (grid[i][j] == '1') {
ret += 1;
// bfs
while (!q.empty())
q.pop();
q.push(i * m + j);
grid[i][j] = '2';
while (!q.empty()) {
int x = q.front() / m;
int y = q.front() % m;
q.pop();
for (int k = 0; k < 4; ++k) {
int newx = x + dx[k];
int newy = y + dy[k];
if (newx < 0 || newx >= n || newy < 0 || newy >= m || grid[newx][newy] != '1')
continue;
grid[newx][newy] = '2';
q.push(newx * m + newy);
}
}
}
}
}
return ret;
}
};
int main() {
vector<vector<char> > grid({{'1', '0', '1', '1', '0', '1', '1'}});
Solution s;
cout << s.numIslands(grid);
return 0;
}