mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
@ -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 数组进行标记
|
||||
@ -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">
|
||||
|
Reference in New Issue
Block a user