1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #include <iostream> #include <queue> #include <algorithm>
#define x first #define y second
using namespace std;
typedef pair<int, int> PII; const int N = 25;
int n, m; char g[N][N];
int bfs(int sx, int sy) { queue<PII> q; q.push({sx, sy}); g[sx][sy] = '#'; int res = 0;
int dx[] = {-1, 0, 1, 0}, dy[] = {0, 1, 0, -1};
while (q.size()) { auto t = q.front(); q.pop(); res ++ ;
for (int i = 0; i < 4; i ++ ) { int x = t.x + dx[i], y = t.y + dy[i]; if (x < 0 || x >= n || y < 0 || y >= m || g[x][y] != '.') continue; g[x][y] = '#'; q.push({x, y}); } } return res; }
int main() { while (cin >> m >> n, n || m) { for (int i = 0; i < n; i ++ ) cin >> g[i]; int x, y; for (int i = 0; i < n; i ++ ) for (int j = 0; j < m; j ++ ) if (g[i][j] == '@') { x = i; y = j; } cout << bfs(x, y) << endl; }
return 0; }
|