Skip to content

Commit 68e0041

Browse files
committed
200. Number of Islands
1 parent df7a782 commit 68e0041

File tree

3 files changed

+190
-0
lines changed

3 files changed

+190
-0
lines changed

200/step1.cpp

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
Solve Time : 42:17
3+
4+
Time : O(NM log NM)
5+
Space : O(NM)
6+
7+
まずBFSな方法を思いつく。
8+
島を探索し、陸を見つけたら関数を呼び出して陸地すべてを探索済みにしてカウントアップする。
9+
初期値として{0, 0}を設定していたが、そこが島だと適切にカウントされないパターンがあったためすべてのマスをまずキューに入れたらpassした
10+
11+
Set<Coordinate>を利用するうえでoperator<の宣言が必要なことに気づけず時間がかかった。
12+
*/
13+
class Solution {
14+
public:
15+
int numIslands(vector<vector<char>>& grid) {
16+
int island_count = 0;
17+
width = grid.front().size();
18+
height = grid.size();
19+
queue<Coordinate> checking_coordinates;
20+
for (int y = 0; y < grid.size(); ++y) {
21+
for (int x = 0; x < grid.front().size(); ++x) {
22+
checking_coordinates.push({x, y});
23+
}
24+
}
25+
int count = 0;
26+
while (!checking_coordinates.empty()) {
27+
Coordinate coordinate = checking_coordinates.front();
28+
checking_coordinates.pop();
29+
if (visited.contains(coordinate)) {
30+
continue;
31+
}
32+
if (grid.at(coordinate.y).at(coordinate.x) == '1') {
33+
CheckIsland(coordinate, grid);
34+
++count;
35+
}
36+
visited.insert(coordinate);
37+
if (coordinate.x - 1 >= 0) checking_coordinates.push({coordinate.x - 1, coordinate.y});
38+
if (coordinate.x + 1 < width) checking_coordinates.push({coordinate.x + 1, coordinate.y});
39+
if (coordinate.y - 1 >= 0) checking_coordinates.push({coordinate.x, coordinate.y - 1});
40+
if (coordinate.y + 1 < height) checking_coordinates.push({coordinate.x, coordinate.y + 1});
41+
}
42+
return count;
43+
}
44+
45+
private:
46+
struct Coordinate {
47+
int x;
48+
int y;
49+
bool operator<(const Coordinate& other) const {
50+
return (y < other.y) || (y == other.y && x < other.x);
51+
}
52+
};
53+
54+
int width;
55+
int height;
56+
set<Coordinate> visited;
57+
58+
void CheckIsland(Coordinate coordinate, const vector<vector<char>>& grid) {
59+
queue<Coordinate> island_coordinates;
60+
island_coordinates.push(coordinate);
61+
while (!island_coordinates.empty()) {
62+
Coordinate coordinate = island_coordinates.front();
63+
island_coordinates.pop();
64+
if (visited.contains(coordinate)) {
65+
continue;
66+
}
67+
visited.insert(coordinate);
68+
if (grid.at(coordinate.y).at(coordinate.x) == '1') {
69+
visited.insert(coordinate);
70+
if (coordinate.x - 1 >= 0) island_coordinates.push({coordinate.x - 1, coordinate.y});
71+
if (coordinate.x + 1 < width) island_coordinates.push({coordinate.x + 1, coordinate.y});
72+
if (coordinate.y - 1 >= 0) island_coordinates.push({coordinate.x, coordinate.y - 1});
73+
if (coordinate.y + 1 < height) island_coordinates.push({coordinate.x, coordinate.y + 1});
74+
}
75+
}
76+
}
77+
};

200/step2.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Time : O(NM)
3+
Space : O(NM)
4+
5+
他者のコードを読んだうえで改良したコード
6+
- マジックナンバーの廃止
7+
- 訪問済みのマスの管理にsetではなく2次元vectorにしたほうが定数アクセスで早いので修正
8+
- 範囲外の判定を一行にまとめて見やすくする
9+
- 島内探索前の外海の探索は全探索にしてコードをシンプルにする
10+
といった点を改良
11+
*/
12+
class Solution {
13+
public:
14+
int numIslands(vector<vector<char>>& grid) {
15+
int island_count = 0;
16+
height = grid.size();
17+
width = grid.front().size();
18+
checked = vector<vector<bool>>(height, vector<bool>(width, false));
19+
for (int row = 0; row < height; ++row) {
20+
for (int col = 0; col < width; ++col) {
21+
if (grid.at(row).at(col) == sea || checked.at(row).at(col)) {
22+
continue;
23+
}
24+
WalkThroughIsland(row, col, grid);
25+
++island_count;
26+
}
27+
}
28+
return island_count;
29+
}
30+
31+
private:
32+
int height;
33+
int width;
34+
vector<vector<bool>> checked;
35+
const char sea = '0';
36+
37+
struct Coordinate {
38+
int col;
39+
int row;
40+
};
41+
42+
void WalkThroughIsland(int row, int col, vector<vector<char>>& grid) {
43+
stack<Coordinate> target_coordinates;
44+
target_coordinates.push({col, row});
45+
while (!target_coordinates.empty()) {
46+
Coordinate coordinate = target_coordinates.top();
47+
target_coordinates.pop();
48+
if (coordinate.col < 0 || width <= coordinate.col || coordinate.row < 0 || height <= coordinate.row) {
49+
continue;
50+
}
51+
if (checked.at(coordinate.row).at(coordinate.col)) {
52+
continue;
53+
}
54+
checked.at(coordinate.row).at(coordinate.col) = true;
55+
if (grid.at(coordinate.row).at(coordinate.col) == sea) {
56+
continue;
57+
}
58+
target_coordinates.push({coordinate.col + 1, coordinate.row});
59+
target_coordinates.push({coordinate.col - 1, coordinate.row});
60+
target_coordinates.push({coordinate.col, coordinate.row + 1});
61+
target_coordinates.push({coordinate.col, coordinate.row - 1});
62+
}
63+
}
64+
};

200/step3.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution {
2+
public:
3+
int numIslands(vector<vector<char>>& grid) {
4+
height = grid.size();
5+
width = grid.front().size();
6+
visited = vector<vector<bool>>(height, vector<bool>(width, false));
7+
int island_count = 0;
8+
for (int row = 0; row < height; ++row) {
9+
for (int col = 0; col < width; ++col) {
10+
if (grid.at(row).at(col) == sea || visited.at(row).at(col)) {
11+
continue;
12+
}
13+
WalkThroughIsland(row, col, grid);
14+
++island_count;
15+
}
16+
}
17+
return island_count;
18+
}
19+
20+
private:
21+
const char sea = '0';
22+
int height, width;
23+
vector<vector<bool>> visited;
24+
25+
struct Coordinate {
26+
int row;
27+
int col;
28+
};
29+
30+
void WalkThroughIsland(int row, int col, vector<vector<char>>& grid) {
31+
stack<Coordinate> next_coordinates;
32+
next_coordinates.emplace(row, col);
33+
while (!next_coordinates.empty()) {
34+
Coordinate coordinate = next_coordinates.top();
35+
next_coordinates.pop();
36+
if (coordinate.row < 0 || coordinate.row >= height || coordinate.col < 0 || coordinate.col >= width) {
37+
continue;
38+
}
39+
if (grid.at(coordinate.row).at(coordinate.col) == sea || visited.at(coordinate.row).at(coordinate.col)) {
40+
continue;
41+
}
42+
visited.at(coordinate.row).at(coordinate.col) = true;
43+
next_coordinates.emplace(coordinate.row + 1, coordinate.col);
44+
next_coordinates.emplace(coordinate.row - 1, coordinate.col);
45+
next_coordinates.emplace(coordinate.row, coordinate.col + 1);
46+
next_coordinates.emplace(coordinate.row, coordinate.col - 1);
47+
}
48+
}
49+
};

0 commit comments

Comments
 (0)