jinyanshao @ 2024-11-01 16:13:43
#include<bits/stdc++.h>
using namespace std;
//#define int long long
int n, m, id;
int mapa[1005][1005];
int vis[1005][1005];
int sx, sy;
int ans[10005];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};
struct node {
int x, y;
};
void bfs(int x, int y) {
queue<node> q;
q.push({x, y});
vis[x][y] = 1;
while (!q.empty()) {
node now = q.front();
q.pop();
// cout << now.x << " " << now.y << endl;
for (int i = 0; i < 4; i++) {
int xx = now.x + dx[i];
int yy = now.y + dy[i];
if (xx < 1 || xx > n || yy < 1 || yy > n || vis[xx][yy] == 1 || mapa[now.x][now.y] == mapa[xx][yy])continue;
ans[id]++;
// cout << xx << " " << yy << endl;
vis[xx][yy] = id;
q.push({xx, yy});
}
}
}
signed main() {
// freopen("P1141_1.in", "r", stdin);
// freopen("A.out", "w", stdout);
// ios::sync_with_stdio(false);
// cin.tie(0);
// cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
char c;
cin >> c;
mapa[i][j] = c - '0';
}
}
// for (int i = 1; i <= n; i++) {
// for (int j = 1; j <= m; j++) {
// cout << mapa[i][j];
// }
// cout << endl;
// }
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (vis[i][j] == 0) {
id++;
bfs(i, j);
}
}
}
while (m--) {
cin >> sx >> sy;
// cout << endl;
cout << ans[vis[sx][sy]] + 1 << endl;
// cout << endl;
}
return 0;
}
//3 3
//010
//101
//010
//1 1
//1 2
//3 1
by r2bxyy @ 2024-11-01 16:30:58
queue爆空间了,前面判断是否加进去的时候有重复,比如说如果你判断vis[x][y]==1的话之前被标记为id的就又重复了 \ @jinyanshao
by jinyanshao @ 2024-11-01 16:32:17
已关