http://acm.hdu.edu.cn/showproblem.php?pid=1241
简单的搜索题。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct abc
{
int x,y;
} queue[10005],e;
int vis[105][105],map[105][105];
int dir[8][2] = {1,0,1,1,0,1,-1,1,-1,0,-1,-1,0,-1,1,-1};
int n,m;
void bfs(int sx,int sy)
{
int start,tail,x,y;
//int flag = 0;
start = tail = 0;
e.x = sx;
e.y = sy;
queue[start] = e;
while (start <= tail)
{
x = queue[start].x;
y = queue[start].y;
for (int i=0;i<8;i++)
{
e.x = x + dir[i][0];
e.y = y + dir[i][1];
if (vis[e.x][e.y] || e.x <= 0 || e.x > m || e.y <= 0 || e.y > n || map[e.x][e.y] == 0)
continue ;
vis[e.x][e.y] = 1;
tail++;
queue[tail] = e;
vis[e.x][e.y] = 1;
}
start++;
}
return ;
}
int main()
{
while (scanf("%d%d",&m,&n) && m)
{
int count = 0;
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
for (int i=1;i<=m;i++)
{
char ss[1000];
scanf("%s",ss);
for (int j=0;j<n;j++)
if (ss[j] == '@')map[i][j+1] = 1;
}
for (int i=1;i<=m;i++)
for (int j=1;j<=n;j++)
if (vis[i][j] == 0 && map[i][j])
{
vis[i][j] = 1;
bfs(i,j);
count++;
}
printf("%d\n",count);
}
return 0;
}