| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
Java 我不理解为啥不可以WAimport 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: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator