LysAL13_ @ 2023-12-16 19:31:21
rt,样例全部输出1
用的手写队列(别问,问就是老师让这么写)
#include<bits/stdc++.h>
using namespace std;
struct node{
int x,y;
}q[100005];
int n,m;
char mp[1005][1005];
int tx,ty,vis[1005][1005];
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
//void dfs(){
// return;
//}
void bfs(int x,int y){
int ans=1,heal=0,tail=1,tx,ty;
q[heal].x=x,q[heal].y=y;
vis[x][y]=1;
while(heal<tail){
for(int i=0;i<=3;i++){
tx=q[heal].x+dx[i];
ty=q[heal].y+dy[i];
if(tx>=1&&tx<=n&&ty>=1&&ty<=n&&vis[tx][ty]==0&&mp[q[heal].x][q[heal].y]==mp[tx][ty])
q[tail].x=tx,q[tail].y=ty,vis[tx][ty]=1,ans++,tail++;
}
heal++;
}
for(int i=0;i<tail;i++)
vis[q[i].x][q[i].y]=ans;
}
int main(){
//freopen("xxx.in","r",stdin);
//freopen("xxx.out","w",stdout);
//ios::sync_with_stdio(false);
//cin.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>mp[i][j];
for(int i=1;i<=m;i++){
cin>>tx>>ty;
if(vis[tx][ty]!=0) cout<<vis[tx][ty]<<endl;
else{bfs(tx,ty);cout<<vis[tx][ty]<<endl;}
}
return 0;
}