反打帅童
2025-08-16 18:59:52
发布于:广东
9阅读
0回复
0点赞
帅童不信能用bfs,那我就写一个AC的bfs给他看看
#include <bits/stdc++.h>
using namespace std;
constexpr int dirs[][2]{1, 0, 0, 1, -1, 0, 0, -1};
string grid[105];
int n, m;
int bfs(int a, int b) {
int cnt = 1;
bool vis[105][105]{};
queue <pair <int, int>> q;
q.emplace(a, b);
vis[a][b] = 1;
while (! q.empty()) {
auto [x, y] = q.front(); q.pop();
int flag = grid[x][y] - '0';
for (auto &[dx, dy] : dirs) {
int nx = x + dx, ny = y + dy;
if (nx < 1 || ny < 1 || nx > n || ny > n) continue;
if (vis[nx][ny]) continue;
if (grid[nx][ny] - '0' == flag) continue;
vis[nx][ny] = 1;
q.push({nx, ny});
cnt ++;
}
}
// for (int i = 1; i <= n; i ++)
// for (int j = 1; j <= n; j ++)
// cout << (vis[i][j] ? "1" : "0") << " \n"[j == n];
return cnt;
}
int main(void) {
cin >> n >> m;
for (int i = 1; i <= n; i ++) {
cin >> grid[i];
grid[i] = ' ' + grid[i];
}
while (m --) {
int x, y;
cin >> x >> y;
cout << bfs(x, y) << '\n';
}
return 0;
}
全部评论 1
1周前 来自 广东
0%%%
1周前 来自 浙江
0居然一开始不相信我的bfs吗
1周前 来自 广东
0bfs难打秒了
1周前 来自 浙江
0
有帮助,赞一个