Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
Register

## Java 我不理解为啥不可以WA

Posted by 113120190215 at 2021-10-17 00:56:16 on Problem 2386
```import java.util.*;
public class Lake {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
char[][] field = new char[n][m];
for(int i = 0; i < n; i++){
String string = in.next();
string.getChars(0, string.length(), field[i], 0);
}
int res = 0;
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
if(field[i][j] == 'W'){
dfs(field,i, j);
res++;
}
}
}
System.out.println(res);
}
static void dfs(char[][] grid, int x, int y){
grid[x][y] = '.';
for(int dx = -1; dx <= 1; dx++){
for(int dy = -1; dy <= 1; dy++){

int nx = x + dx;
int ny = y + dy;

if(0 <= nx && nx < grid.length && 0 <= ny && ny < grid[0].length && grid[nx][ny] == 'W'){
dfs(grid, nx, ny);
}

}
}

//			if(x < 0||x >= grid.length||y < 0||y >= grid[0].length||grid[x][y] != 'W'||
//					grid[x][y] == '.'){
//				return;
//			}
//			grid[x][y] = '.';
//			dfs(grid, x+1, y);
//			dfs(grid, x-1, y);
//			dfs(grid, x+1, y+1);
//			dfs(grid, x-1, y-1);
//			dfs(grid, x, y+1);
//			dfs(grid, x, y-1);
//			dfs(grid, x-1, y+1);
//			dfs(grid, x+1, y-1);
//			return;
}
}
```

Followed by: