题目描述
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid.
Zero在玩消消乐的游戏,游戏规则非常简单,Zero仅可以消除@字符所在区域。而一次消除操作可以消除相连着的一整块的@(相连是指 88 个方向,横向相邻、纵向相邻或对角相邻)。
给你一个游戏区域,请问Zero需要进行多少次消除操作才可以消灭全部的@呢?
Input
The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.
多组数据读入,每组数据在第一行给出两个整数 nn 和 mm,表示行和列,1 \le n,m \le 1001≤n,m≤100,如果 m = 0m=0 表示输入的结束,接下来是 nn 行,每行 mm 个字符。每个字符要么是'*',要么是'@'。
Output
For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.
对于每一个游戏区域,在一行输出进行消灭操作的数量,每个结果输出占一行。
每行输出末尾不能有多余空格。
Sample
Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Output
0
1
2
2
dfs
static class TaskA {
int sum = 0;
int n,m;
int[][] wei = {{1,0},{-1,0},{0,1},{0,-1},{-1,-1},{-1,1},{1,1},{1,-1}};
void dfs(int x, int y,char[][] a,int sum) {
for (int k = 0; k < 8; k++) {//枚举八个方向的走法
//计算下一个点的坐标
int nx = x + wei[k][0];
int ny = y + wei[k][1];
if(nx >= 0 && nx < n && ny >= 0&& ny < m){//不超界情况下走下一个点
if(a[nx][ny] == '@'){
a[nx][ny] = '*';//把该点标记为已经走过
dfs(nx,ny,a,++sum);
}
}
}
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
while (in.hasNext()) {
n = in.nextInt();
m = in.nextInt();
if (m == 0) {
break;
}else{
char[][] a = new char[n][m];//存储输入数据
for (int i = 0; i < n; i++) {
String s = in.next();
for (int j = 0; j < m; j++) {
a[i][j] = s.charAt(j);
}
}
sum = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == '@') {
a[i][j] = '*';
dfs(i,j,a,++sum);
}
}
}
out.println(sum);
}
}
}
}