搜索, dfs、bfs都可以
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#include <cmath>
#include <vector>
#include <set>
#include <stack>
#include <algorithm>
//#include "myAlgorithm.h"
#define MAX 25
#define INF (1e8 + 5)
#define eps 1e-8
#define Rep(s, e) for( int i = s; i <= e; i++)
#define Cep(e, s) for( int i = e; i >= s; i --)
using namespace std;
int y, x, si, sj;
char str[MAX][MAX];
int dir[][2] = {{1, 0},{-1, 0},{0, 1},{0, -1}};
struct Point{
int xx, yy;
}tt, temp;
void bfs(){
int ans = 0;
queue<Point>q;
tt.xx = sj; tt.yy = si;
str[si][sj] = '#'; //set red
q.push(tt);
while(!q.empty()){
temp = q.front(); q.pop();
ans ++;
Rep(0, 3){
int I = dir[i][0] + temp.yy, J = dir[i][1] + temp.xx;
if(I > -1 && J > -1 && I < y && J < x && str[I][J] == '.'){
tt.yy = I; tt.xx = J;
str[I][J] = '#'; // set red;
q.push(tt);
}
}
}
cout<<ans<<endl;
}
int main()
{
//freopen("in.tx0t", "w", stdout);
while(cin>>x>>y, x + y){
getchar();
si = -1;
Rep(0, y - 1){
gets(str[i]);
if(si == -1){
for(int j = 0; j < x; j++){
if(str[i][j] == '@'){
si = i; sj = j;break;
}
}
}///get start pos;
}///end input
bfs();
}
return 0;
}
/*
*/