Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #301

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 9 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jul 14, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 124 additions & 2 deletions problems/1020.飞地的数量.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,63 @@ public:

### Java

深度优先遍历版本:
深度优先遍历(没有终止条件 + 空間優化(淹沒島嶼,沒有使用visited數組))
```java
//DFS
class Solution {
int count = 0;
int[][] dir ={
{0, 1},
{1, 0},
{-1, 0},
{0, -1}
};
private void dfs(int[][] grid, int x, int y){
if(grid[x][y] == 0)
return;

grid[x][y] = 0;
count++;

for(int i = 0; i < 4; i++){
int nextX = x + dir[i][0];
int nextY = y + dir[i][1];

if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length)
continue;
dfs(grid, nextX, nextY);
}

}

public int numEnclaves(int[][] grid) {
for(int i = 0; i < grid.length; i++){
if(grid[i][0] == 1)
dfs(grid, i, 0);
if(grid[i][grid[0].length - 1] == 1)
dfs(grid, i, grid[0].length - 1);
}
//初始化的時候,j 的上下限有調整過,必免重複操作。
for(int j = 1; j < grid[0].length - 1; j++){
if(grid[0][j] == 1)
dfs(grid, 0, j);
if(grid[grid.length - 1][j] == 1)
dfs(grid, grid.length - 1, j);
}
count = 0;

for(int i = 1; i < grid.length - 1; i++){
for(int j = 1; j < grid[0].length - 1; j++){
if(grid[i][j] == 1)
dfs(grid, i, j);
}
}
return count;
}
}
```

深度优先遍历(没有终止条件)

```java
class Solution {
Expand Down Expand Up @@ -206,7 +262,7 @@ class Solution {
}
```

广度优先遍历版本:
广度优先遍历(使用visited數組)

```java
class Solution {
Expand Down Expand Up @@ -269,6 +325,72 @@ class Solution {
}
```

廣度优先遍历(空間優化(淹沒島嶼,沒有使用visited數組))
```java
//BFS
class Solution {
int count = 0;
int[][] dir ={
{0, 1},
{1, 0},
{-1, 0},
{0, -1}
};
private void bfs(int[][] grid, int x, int y){
Queue<Integer> que = new LinkedList<>();
que.offer(x);
que.offer(y);
count++;
grid[x][y] = 0;

while(!que.isEmpty()){
int currX = que.poll();
int currY = que.poll();

for(int i = 0; i < 4; i++){
int nextX = currX + dir[i][0];
int nextY = currY + dir[i][1];

if(nextX < 0 || nextY < 0 || nextX >= grid.length || nextY >= grid[0].length)
continue;

if(grid[nextX][nextY] == 1){
que.offer(nextX);
que.offer(nextY);
count++;
grid[nextX][nextY] = 0;
}
}
}
}

public int numEnclaves(int[][] grid) {
for(int i = 0; i < grid.length; i++){
if(grid[i][0] == 1)
bfs(grid, i, 0);
if(grid[i][grid[0].length - 1] == 1)
bfs(grid, i, grid[0].length - 1);
}
for(int j = 1; j < grid[0].length; j++){
if(grid[0][j] == 1)
bfs(grid, 0 , j);
if(grid[grid.length - 1][j] == 1)
bfs(grid, grid.length - 1, j);
}
count = 0;
for(int i = 1; i < grid.length - 1; i++){
for(int j = 1; j < grid[0].length - 1; j++){
if(grid[i][j] == 1)
bfs(grid,i ,j);
}
}
return count;
}
}


```

### Python

深度优先遍历
Expand Down

AltStyle によって変換されたページ (->オリジナル) /