基础的BFS,把STL的队列改成手写队列了,搜索结果也另外存储了,不知道还能怎么优化
#include <bits/stdc++.h>
using namespace std;
int n,m,ans;
int mp[1005][1005],a[1005][1005];
bool vis[1005][1005];
int l,r;
struct node {
int x,y;
}q[1000004];
int dx[5] = {0,1,-1,0,0},dy[5]= {0,0,0,-1,1};
inline void bfs(int x,int y) {
++ans;
vis[x][y] = true;
q[r++] = node{x,y};
while(!(l == r)) {
node t = q[l];
++l;
for (int i = 1; i <= 4; i++) {
int tx =t.x +dx[i],ty = t.y+dy[i];
if (tx < 1 || ty < 1 || tx > n || ty > n) continue;
if (mp[tx][ty] == mp[t.x][t.y]) continue;
if (vis[tx][ty]) continue;
vis[tx][ty] = true;
if (a[tx][ty] != 0) {
ans += a[tx][ty];
continue;
}
++ans;
q[r++] = node{tx,ty};
}
}
}
int main () {
cin >> n >> m;
for (int i = 1; i <= n ;i++) {
for (int j = 1; j <= n; j++) {
char c;
cin >> c;
mp[i][j] =c - '0';
}
}
while (m--) {
int x,y;
cin >> x >> y;
if (a[x][y] != 0 ) {
cout << a[x][y] << endl;
continue;
}
ans = 0;
memset(vis,0,sizeof(vis));
memset(q,0,sizeof(q));
l = 0,r = 0;
bfs(x,y);
for (int i = 0; i <= r; i++) {
if (a[q[i].x][q[i].y] == 0) a[q[i].x][q[i].y] = ans;
}
cout << ans << endl;
}
return 0;
}