You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.0 KiB

#include <bits/stdc++.h>
using namespace std;
const int N = 25;
// dfs 实现flood fill 算法
int n, m;
char g[N][N];
bool st[N][N];
int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};
int cnt;
void dfs(int x, int y) {
// x,y贡献了1个结点数
cnt++;
st[x][y] = true;
for (int i = 0; i < 4; i++) {
int tx = x + dx[i], ty = y + dy[i];
if (tx < 0 || tx >= n || ty < 0 || ty >= m) continue;
if (g[tx][ty] != '.') continue;
if (st[tx][ty]) continue;
dfs(tx, ty);
}
}
int main() {
while (cin >> m >> n, n || m) { // 先输入列数,再输入行数,小坑
// 多组测试数据
memset(st, 0, sizeof st);
cnt = 0;
// 找起点
int x, y;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++) {
cin >> g[i][j];
if (g[i][j] == '@') x = i, y = j;
}
dfs(x, y);
printf("%d\n", cnt);
}
return 0;
}