从有'@'的地方开始dfs,并将走过的标记为'*',遍历八个方向
#include <iostream>
using namespace std;
const int MAXN = 101;
char field[MAXN][MAXN];
int m, n;
void dfs(int x, int y)
{
field[x][y] = '*';
for(int dx = -1; dx <= 1; dx++)
{
for(int dy = -1; dy <= 1; dy++)
{
int nx = x + dx, ny = y + dy;
if(0 <= nx && nx < m && 0 <= ny && ny < n && field[nx][ny] == '@')
{
dfs(nx, ny);
}
}
}
}
int main()
{
while(cin >> m >> n && m != 0)
{
// char field[m][n];
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
cin >> field[i][j];
}
}
int res = 0;
for(int i = 0; i < m; i++)
{
for(int j = 0; j < n; j++)
{
if(field[i][j] == '@')
{
dfs(i, j);
res++;
}
}
}
cout << res << endl;
}
return 0;
}