水得不要不要的,直接暴力,主要是读入要思考一下,然后就没有然后了。。。
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
#define For(i, n, m) for(i = n;i <= m; ++i)
#define Fod(i, n, m) for(i = n;i >= m; --i)
using namespace std;
const int maxn = 1000;
int map[maxn][maxn], p[maxn][maxn];
int n, m, ans;
int dp[4][2] = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
void dfs(int x, int y) {
int i;
For(i, 0, 3) {
int u = x + dp[i][0];
int v = y + dp[i][1];
if(!map[u][v])
continue;
if(p[u][v])
continue;
if(u < 1 || u > m || v < 1 || v > n)
continue;
p[u][v] = 1;
++ans;
dfs(u, v);
}
}
int main() {
int i, j;
while(1) {
scanf("%d%d", &n, &m);
if(n == 0 && m == 0)
return 0;
int x, y;
For(i, 1, m)
For(j, 1, n) {
char c;
cin>>c;
if(c == '@') {
x = i;
y = j;
map[i][j] = 1;
}
else if(c == '.')
map[i][j] = 1;
else if(c == '#')
map[i][j] = 0;
}
memset(p, 0, sizeof(p));
p[x][y] = 1;
ans = 1;
dfs(x, y);
printf("%d\n", ans);
}
return 0;
}