AcWing 1113. 红与黑
原题链接
简单
作者:
回归线
,
2021-03-28 23:31:57
,
所有人可见
,
阅读 217
#include <iostream>
#include <queue>
using namespace std;
const int N = 25;
char g[N][N];
int m, n;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
int bfs(int x, int y) {
queue<pair<int, int>> q;
q.push({x, y});
g[x][y] = '#';
int res = 0;
while (!q.empty()) {
auto t = q.front();
q.pop();
++res;
for (int d = 0; d < 4; ++d) {
int a = t.first + dx[d];
int b = t.second + dy[d];
if (a < 0 || a >= m || b < 0 || b >= n || g[a][b] != '.') {
continue;
}
g[a][b] = '#';
q.push({a, b});
}
}
return res;
}
int main() {
while (cin >> n >> m, n || m) {
int sx, sy;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
cin >> g[i][j];
if (g[i][j] == '@') {
sx = i;
sy = j;
}
}
}
cout << bfs(sx, sy) << endl;
}
return 0;
}