r******j 发帖数: 92 | 1 下面是是我的dfs解法,可是过不了test,哪位能帮我看一下错在哪里了吗。
public class Solution {
public void solve(char[][] board) {
// Note: The Solution object is instantiated only once and is reused
by each test case.
int row = board.length;
if (row == 0) {
return;
}
int column = board[0].length;
for (int i = 0; i < row; i++) {
dfs(board, i, 0);
dfs(board,i, column - 1);
}
for (int j = 1; j < column - 1; j++) {
dfs(board, 0, j);
dfs(board,row - 1, j);
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
if (board[i][j] == 'O') {
board[i][j] = 'X';
} else if (board[i][j] == 'T'){
board[i][j] = 'O';
}
}
}
return;
}
public void dfs(char[][] board, int i, int j) {
int row = board.length;
int column = board[0].length;
if (board[i][j] == 'O') {
board[i][j] = 'T';
if (i + 1 < row) {
dfs(board, i+ 1, j);
}
if (j + 1 < column) {
dfs(board, i, j + 1);
}
if (i - 1 >= 0) {
dfs(board, i-1, j);
}
if (j - 1 >= 0) {
dfs(board, i, j-1);
}
}
return;
}
} | j*********6 发帖数: 407 | 2 dfs 写对了 好像也过不了 应该是递归太深的原因 改成bfs吧~ | r******j 发帖数: 92 | 3 恩恩,谢谢你啊~
【在 j*********6 的大作中提到】 : dfs 写对了 好像也过不了 应该是递归太深的原因 改成bfs吧~
|
|