bfs
#include <bits/stdc++.h>
using namespace std;
const int N=25;
int w, h, tt, hh;
char g[N][N];
pair<int,int> q[N*N];
int dx[]={1,0,-1,0}, dy[]={0,-1,0,1};
int bfs(int x, int y) {
hh=0, tt=0, q[0]={x, y};
int res=0;
g[y][x]='#';
while (hh<=tt) {
res++;
auto cur=q[hh++];
for (int i=0; i<4; i++) {
int a=cur.first+dx[i], b=cur.second+dy[i];
if (a>=0 && a<w && b>=0 && b<h && g[b][a]=='.') {
g[b][a]='#';
q[++tt]={a, b};
}
}
}
return res;
}
int main() {
while (cin>>w>>h, w||h) {
int x, y;
for (int i=0; i<h; i++) for (int j=0; j<w; j++) {
cin>>g[i][j];
if (g[i][j]=='@') x=j, y=i;
}
cout<<bfs(x, y)<<endl;
}
return 0;
}
dfs
#include <bits/stdc++.h>
using namespace std;
const int N=25;
int w, h;
char g[N][N];
int dx[]={1, 0, -1, 0}, dy[]={0, -1, 0, 1};
int dfs(int x, int y) {
int res=1;
g[y][x]='#';
for (int i=0; i<4; i++) {
int a=x+dx[i], b=y+dy[i];
if (a>=0 && a<w && b>=0 && b<h && g[b][a]=='.') res+=dfs(a, b);
}
return res;
}
int main() {
while (cin>>w>>h, w||h) {
int x, y;
for (int i=0; i<h; i++) for (int j=0; j<w; j++) {
cin>>g[i][j];
if (g[i][j]=='@') x=j, y=i;
}
cout<<dfs(x, y)<<'\n';
}
return 0;
}