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 1 commit
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
Prev Previous commit
Next Next commit
新增JAVA解法
1. java的BFS解法有寫一個helper function去呼叫
2. java's DFS with 終止條件
  • Loading branch information
Lozakaka authored Jun 27, 2023
commit cdc5a1e9e47690fee5c5f5b34d66f643d6e08907
89 changes: 89 additions & 0 deletions problems/0130.被围绕的区域.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,54 @@ class Solution {
}
}
```
```Java
//BFS(使用helper function)
class Solution {
int[][] dir ={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public void solve(char[][] board) {
for(int i = 0; i < board.length; i++){
if(board[i][0] == 'O') bfs(board, i, 0);
if(board[i][board[0].length - 1] == 'O') bfs(board, i, board[0].length - 1);
}

for(int j = 1 ; j < board[0].length - 1; j++){
if(board[0][j] == 'O') bfs(board, 0, j);
if(board[board.length - 1][j] == 'O') bfs(board, board.length - 1, j);
}

for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == 'A') board[i][j] = 'O';
}
}
}
private void bfs(char[][] board, int x, int y){
Queue<Integer> que = new LinkedList<>();
board[x][y] = 'A';
que.offer(x);
que.offer(y);

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 >= board.length || nextY >= board[0].length)
continue;
if(board[nextX][nextY] == 'X'|| board[nextX][nextY] == 'A')
continue;
bfs(board, nextX, nextY);
}
}
}
}

```

```Java
// 深度优先遍历
// 使用 visited 数组进行标记
Expand Down Expand Up @@ -296,6 +344,47 @@ class Solution {
}
}
```
```java
//DFS(有終止條件)
class Solution {
int[][] dir ={{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public void solve(char[][] board) {

for(int i = 0; i < board.length; i++){
if(board[i][0] == 'O') dfs(board, i, 0);
if(board[i][board[0].length - 1] == 'O') dfs(board, i, board[0].length - 1);
}

for(int j = 1 ; j < board[0].length - 1; j++){
if(board[0][j] == 'O') dfs(board, 0, j);
if(board[board.length - 1][j] == 'O') dfs(board, board.length - 1, j);
}

for(int i = 0; i < board.length; i++){
for(int j = 0; j < board[0].length; j++){
if(board[i][j] == 'O') board[i][j] = 'X';
if(board[i][j] == 'A') board[i][j] = 'O';
}
}
}

private void dfs(char[][] board, int x, int y){
if(board[x][y] == 'X'|| board[x][y] == 'A')
return;
board[x][y] = 'A';
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 >= board.length || nextY >= board[0].length)
continue;
// if(board[nextX][nextY] == 'X'|| board[nextX][nextY] == 'A')
// continue;
dfs(board, nextX, nextY);
}
}
}
```

<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down

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