注意数据的读入。
简单题。
#include <stdio.h>
#include <string.h>
int r, c;
char map[101][101];
// # have visited
int dx[] = { 0,0,1,1,1,-1,-1,-1 };
int dy[] = { -1,1,-1,0,1,-1,0,1 };
int count;
void dfs(int x, int y)
{
map[x][y] = '#';
int i;
int tx, ty;
for (i = 0; i <= 7; i++)
{
tx = x + dx[i];
ty = y + dy[i];
if (tx > 0 && ty > 0 && ty <= c&&tx <= r&&map[tx][ty] == '@')
dfs(tx, ty);
}
}
int main()
{
int i, j;
while (EOF != scanf("%d %d", &r, &c) && (r || c))
{
getchar();
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
scanf("%c", &map[i][j]);
}
getchar();
}
count = 0;
for (i = 1; i <= r; i++)
{
for (j = 1; j <= c; j++)
{
if (map[i][j] == '@')
{
dfs(i, j);
count++;
}
}
}
printf("%d\n", count);
}
return 0;
}