广搜的模板题,甚至和书上的例题一模一样,变都没变。。改了下用结构体做。
AC代码:
#include<stdio.h>
const int N = 100 + 5;
struct point {
char ch;
bool vis;
};
int count;
point p[N][N];
void init () {
for (int i = 0 ; i < N ; i++)
for ( int j = 0; j < N ; j++) {
p[i][j].ch = '*';
p[i][j].vis = false;
}
count = 0;
}
void dfs(int x ,int y) {
if(p[x][y].ch == '*' || p[x][y].vis == true)
return ;
p[x][y].vis = true;
dfs(x - 1, y - 1);
dfs(x - 1,y);
dfs(x - 1,y + 1);
dfs(x ,y - 1);
dfs(x , y + 1);
dfs (x + 1 , y - 1);
dfs (x + 1 , y);
dfs (x + 1,y + 1);
}
int main () {
int n,m;
while (scanf("%d%d",&n,&m)) {
getchar();
if (n == 0 && m == 0)
break;
init();
for ( int i = 1 ;i <= n ; i++) {
for (int j = 1 ;j <= m ;j++){
scanf("%c",&p[i][j].ch);
}
getchar();
}
for (int i = 1; i <= n ;i++) {
for (int j = 1; j<= m;j++) {
if (p[i][j].ch == '@' && p[i][j].vis == false) {
count++;
dfs(i,j);
}
}
}
printf("%d\n",count);
}
return 0 ;
}