-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path200314-2.cpp
89 lines (87 loc) · 3.13 KB
/
200314-2.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
// https://leetcode-cn.com/problems/number-of-islands/
#include <iostream>
#include <vector>
#include <list>
using namespace std;
class Solution {
public:
int numIslands(vector<vector<char>>& grid) {
int m = grid.size();
if (m <= 0) return 0;
int n = grid.front().size();
if (n <= 0) return 0;
int c = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (grid[i][j] == '1') {
++c;
fillIsland(grid, m, n, i, j);
}
}
}
return c;
}
private:
void fillIsland(vector<vector<char>>& grid, int m, int n, int i, int j) {
list<pair<int, int>> q{{i,j}};
while (!q.empty()) {
auto h = q.front(); q.pop_front();
i = h.first; j = h.second;
if (grid[i][j] == '1') {
grid[i][j] = '0';
if (i > 0 ) q.push_back(make_pair(i - 1, j));
if (i < m - 1) q.push_back(make_pair(i + 1, j));
if (j > 0 ) q.push_back(make_pair(i, j - 1));
if (j < n - 1) q.push_back(make_pair(i, j + 1));
}
}
}
};
int main()
{
Solution s;
{
vector<vector<char>> a = {
{'1','1','1','1','0'},
{'1','1','0','1','0'},
{'1','1','0','0','0'},
{'0','0','0','0','0'},
};
cout << s.numIslands(a) << endl; // answer: 1
}
{
vector<vector<char>> a = {
{'1','1','0','0','0'},
{'1','1','0','0','0'},
{'0','0','1','0','0'},
{'0','0','0','1','1'},
};
cout << s.numIslands(a) << endl; // answer: 3
}
{
vector<vector<char>> a = {
{'1','1','1','1','1','0','1','1','1','1','1','1','1','1','1','0','1','0','1','1'},
{'0','1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','1','0'},
{'1','0','1','1','1','0','0','1','1','0','1','1','1','1','1','1','1','1','1','1'},
{'1','1','1','1','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},
{'1','0','0','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},
{'1','0','1','1','1','1','1','1','0','1','1','1','0','1','1','1','0','1','1','1'},
{'0','1','1','1','1','1','1','1','1','1','1','1','0','1','1','0','1','1','1','1'},
{'1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','0','1','1'},
{'1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','1','1','1','1','1'},
{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},
{'0','1','1','1','1','1','1','1','0','1','1','1','1','1','1','1','1','1','1','1'},
{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},
{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},
{'1','1','1','1','1','0','1','1','1','1','1','1','1','0','1','1','1','1','1','1'},
{'1','0','1','1','1','1','1','0','1','1','1','0','1','1','1','1','0','1','1','1'},
{'1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','1','1','0'},
{'1','1','1','1','1','1','1','1','1','1','1','1','1','0','1','1','1','1','0','0'},
{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},
{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'},
{'1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1','1'}
};
cout << s.numIslands(a) << endl; // answer: 1
}
return 0;
}