摘要:BFS
#include <iostream>
#include <stdio.h>
#include <queue>
using namespace std;
const int size = 20;
int input[size+1][size+1] = {0};
int flag[size+1][size+1] = {0};
int sum = 0;
struct Pos{
int x;
int y;
};
Pos getNextPos(Pos cur, int dir)
{
Pos next;
if( dir == 1){
next.x = cur.x;
next.y = cur.y-1;
}
if( dir == 2){
next.x = cur.x+1;
next.y = cur.y;
}
if( dir == 3 ){
next.x = cur.x;
next.y = cur.y+1;
}
if( dir == 4 ){
next.x = cur.x-1;
next.y = cur.y;
}
return next;
}
int main()
{
int W, H;
while( cin >> W >> H ){
if( W== 0 ){
break;
}
char ch;
Pos start;
for(int i=1; i<=H; i++){
for(int j=1; j<=W; j++){
cin >> ch;
input[i][j] = (ch=='#'?0:1);
flag[i][j] = 1;
if(ch == '@'){
start.x = j;
start.y = i;
}
}
}
queue<Pos> q;
q.push(start);
flag[start.y][start.x] = 0;
sum = 1;
while( !q.empty() ){
Pos cur_pos = q.front();
q.pop();
for(int i=1; i<=4; i++){
Pos next_pos = getNextPos(cur_pos, i);
if( next_pos.x<1 || next_pos.x>W || next_pos.y<1 || next_pos.y>H ){
continue;
}
if( input[next_pos.y][next_pos.x]==0 || flag[next_pos.y][next_pos.x]==0){
continue;
}
sum++;
q.push(next_pos);
flag[next_pos.y][next_pos.x] = 0;
}
}
cout << sum << endl;
}
return 0;
}