AquaDaMean1e @ 2024-08-15 11:02:46
#include <bits/stdc++.h>
using namespace std;
struct node {
int x, y;
};
int cnt;
int dx[5] = {0, -1, 0, 1, 0},
dy[5] = {0, 0, 1, 0, -1};
char ch[1010][1010];
bool vis[1010][1010];
int main () {
int n, m;
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> ch[i][j];
}
}
while (m--) {
int x, y;
queue <node> q;
cin >> x >> y;
q.push({x, y});
cnt = 1;
memset(vis, 0, sizeof(vis));
vis[x][y] = true;
while (!q.empty()) {
node cur = q.front();
q.pop();
for (int i = 1; i <= 4; i++) {
int nx = cur.x + dx[i], ny = cur.y + dy[i];
if (nx && nx <= n && ny && ny <= n && ch[nx][ny] != ch[cur.x][cur.y] && !vis[nx][ny]) {
q.push({nx, ny});
vis[nx][ny] = true;
cnt++;
}
}
}
cout << cnt << endl;
}
}
by Smoke_Clouds_ @ 2024-08-15 17:00:08
你这个是每询问一次就跑一次广搜,应该先跑完,然后询问就直接调用
by Causality_ @ 2024-08-16 11:22:07
@infinity528 读好迷宫之后直接bfs,全存下来,询问直接调用