-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path200_Number of Islands.cpp
More file actions
111 lines (101 loc) · 2.88 KB
/
200_Number of Islands.cpp
File metadata and controls
111 lines (101 loc) · 2.88 KB
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
// Given a 2d grid map of '1's (land) and '0's (water), count the number of islands.
// An island is surrounded by water and is formed by connecting adjacent lands horizontally
// or vertically. You may assume all four edges of the grid are all surrounded by water.
// Example 1:
// 11110
// 11010
// 11000
// 00000
// Answer: 1
#include <iostream>
#include <vector>
using namespace std;
class Solution { // union find
public:
int numIslands(vector<vector<char>>& grid) {
// corner cases
if (grid.size() == 0 || grid[0].size() == 0) return 0;
// init the parent
int count = 0;
row = grid.size(), col = grid[0].size();
parent = vector<vector<int> >(row, vector<int>(col));
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if (grid[i][j] == '0') continue;
parent[i][j] = col * i + j;
++count;
}
}
// group the islands
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
if (grid[i][j] == '0') continue;
if (i + 1 < row && grid[i + 1][j] == '1' && find(i, j) != find(i + 1, j)) {
unions(i, j, i + 1, j); --count;
}
if (j + 1 < col && grid[i][j + 1] == '1' && find(i, j) != find(i, j + 1)) {
unions(i, j, i, j + 1); --count;
}
}
}
// final result
return count;
}
int find(int i, int j) {
if (parent[i][j] == col * i + j) return parent[i][j];
int index = parent[i][j];
parent[i][j] = parent[index / col][index % col];
return find(parent[i][j] / col, parent[i][j] % col);
}
void unions(int i1, int j1, int i2, int j2) {
int index1 = find(i1, j1);
parent[index1 / col][index1 % col] = find(i2, j2);
}
private:
int row, col;
vector<vector<int> > parent;
};
class Solution2 { // dfs
public:
int numIslands(vector<vector<char>>& grid) {
int count = 0, row, column, size;
// calculate the size of grid
if (grid.size() != 0 && grid[0].size() != 0) {
row = grid.size(); column = grid[0].size();
size = row * column;
} else {
return count;
}
for (int i = 0; i < row; ++i) {
for (int j = 0; j < column; ++j) {
if (grid[i][j] == '1') {
++count;
dfs(grid, i, j, row, column);
}
}
}
return count;
}
private:
void dfs(vector<vector<char> >& grid, int i, int j, int& row, int& column) {
grid[i][j] = '0';
int di[4] = {0, 0, 1, -1};
int dj[4] = {1, -1, 0, 0};
for (int k = 0; k < 4; ++k) {
if (i + di[k] >= 0 && i + di[k] < row
&& j + dj[k] >= 0 && j + dj[k] < column
&& grid[i + di[k]][j + dj[k]] == '1') {
dfs(grid, i + di[k], j + dj[k], row, column);
}
}
}
};
int main() {
vector<vector<char> > grid(4, vector<char>(5, '0'));
grid[0][0] = grid[0][1] = grid[0][2] = grid[0][3] = '1';
grid[1][0] = grid[1][1] = grid[1][3] = '1';
grid[2][0] = grid[2][1] = '1';
Solution s;
cout << s.numIslands(grid) << endl;
return 0;
}