岛屿问题

岛屿问题

labuladong

200. 岛屿数量

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
class Solution {
public:
void dfs(vector<vector<char>>& grid, int i, int j, int m, int n) {
if (i < 0 || i > m - 1 || j < 0 || j > n - 1) {
return;
}
if (grid[i][j] == '0') {
return;
}
grid[i][j] = '0';
dfs(grid, i - 1, j, m, n);
dfs(grid, i + 1, j, m, n);
dfs(grid, i, j - 1, m, n);
dfs(grid, i, j + 1, m, n);
}
int numIslands(vector<vector<char>>& grid) {
int result = 0;
int m = grid.size();
int n= grid[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == '1') {
result++;
dfs(grid, i, j, m, n);
}
}
}
return result;
}
};

1254. 统计封闭岛屿的数目

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
class Solution {
public:
void dfs(vector<vector<int>>& grid, int i, int j, int m, int n) {
if (i < 0 || i > m - 1 || j < 0 || j > n - 1) {
return;
}
if (grid[i][j] == 1) {
return;
}
grid[i][j] = 1;
dfs(grid, i - 1, j, m, n);
dfs(grid, i + 1, j, m, n);
dfs(grid, i, j - 1, m, n);
dfs(grid, i, j + 1, m, n);
}
int closedIsland(vector<vector<int>>& grid) {
int result = 0;
int m = grid.size();
int n = grid[0].size();
for (int i = 0; i < m; i++) {
dfs(grid, i, 0, m, n);
dfs(grid, i, n - 1, m, n);
}
for (int i = 0; i < n; i++) {
dfs(grid, 0, i, m, n);
dfs(grid, m - 1, i, m, n);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0) {
result++;
dfs(grid, i, j, m, n);
}
}
}
return result;
}
};

1020. 飞地的数量

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
class Solution {
public:
void dfs(vector<vector<int>>& grid, int i, int j, int m, int n) {
if (i < 0 || i > m - 1 || j < 0 || j > n - 1) {
return;
}
if (grid[i][j] == 0) {
return;
}
grid[i][j] = 0;
dfs(grid, i - 1, j, m, n);
dfs(grid, i + 1, j, m, n);
dfs(grid, i, j - 1, m, n);
dfs(grid, i, j + 1, m, n);
}

int numEnclaves(vector<vector<int>>& grid) {
int result = 0;
int m = grid.size();
int n = grid[0].size();
for (int i = 0; i < m; i++) {
dfs(grid, i, 0, m, n);
dfs(grid, i, n - 1, m, n);
}
for (int i = 0; i < n; i++) {
dfs(grid, 0, i, m, n);
dfs(grid, m - 1, i, m, n);
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
result += (grid[i][j] == 1);
}
}
return result;
}
};

695. 岛屿的最大面积

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
class Solution {
public:
int maxArea = 0;
int dfs(vector<vector<int>>& grid, int i, int j, int m, int n) {
if (i < 0 || i > m - 1 || j < 0 || j > n - 1) {
return 0;
}
if (grid[i][j] == 0) {
return 0;
}
grid[i][j] = 0;
return dfs(grid, i - 1, j, m, n) + dfs(grid, i + 1, j, m, n) + dfs(grid, i, j - 1, m, n) + dfs(grid, i, j + 1, m, n) + 1;
}

int maxAreaOfIsland(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1) {
maxArea = max(maxArea, dfs(grid, i, j, m, n));
}
}
}
return maxArea;
}
};

1905. 统计子岛屿

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
class Solution {
public:
bool dfs(const vector<vector<int>>& grid1, vector<vector<int>>& grid2, int i, int j, int m, int n) {
if (i < 0 || i > m - 1 || j < 0 || j > n - 1) {
return true;
}
if (grid2[i][j] == 0) {
return true;
}
if (grid1[i][j] == 0) {
return false;
}
grid2[i][j] = 0;
bool a = dfs(grid1, grid2, i - 1, j, m, n);
bool b = dfs(grid1, grid2, i + 1, j, m, n);
bool c = dfs(grid1, grid2, i, j - 1, m, n);
bool d = dfs(grid1, grid2, i, j + 1, m, n);
return a && b && c && d;
}

int countSubIslands(vector<vector<int>>& grid1, vector<vector<int>>& grid2) {
int result = 0;
int m = grid1.size();
int n = grid1[0].size();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid1[i][j] && grid2[i][j]) {
result += dfs(grid1, grid2, i, j, m, n);
}
}
}
return result;
}
};

694. 不同的岛屿数量

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
int numDistinctIslands(int[][] grid) {

int m = grid.length, n = grid[0].length;

// 记录所有岛屿的序列化结果

HashSet<String> islands = new HashSet<>();

for (int i = 0; i < m; i++) {

for (int j = 0; j < n; j++) {

if (grid[i][j] == 1) {

// 淹掉这个岛屿,同时存储岛屿的序列化结果

StringBuilder sb = new StringBuilder();

// 初始的方向可以随便写,不影响正确性

dfs(grid, i, j, sb, 666);

islands.add(sb.toString());

}

}

}

// 不相同的岛屿数量

return islands.size();

}
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×