lucy2012 @ 2024-06-10 21:24:47
```cpp
#include<bits/stdc++.h>
using namespace std;
char c;
int n,m,a[1010][1010],vis[1010][1010],next,color[10000010];
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
struct node{
int x,y;
};
int bfs(int x,int y){
if(vis[x][y]!=-1)
return color[vis[x][y]];
int cnt=1;
queue<node> q;
vis[x][y]=next;
node nd={x,y};
q.push(nd);
while(!q.empty()){
node no=q.front();
q.pop();
for(int i=0;i<4;i++){
int xx=no.x+dx[i];
int yy=no.y+dy[i];
if(!(xx>0&&xx<=n&&yy>0&&yy<=n))
continue;
if(vis[xx][yy]==-1&&a[xx][yy]!=a[x][y]){
vis[xx][yy]=next;
cnt++;
q.push(no);
}
}
}
color[next]=cnt;
next++;
return cnt;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>c;
a[i][j]=c-'0';
}
}
memset(vis,-1,sizeof(vis));
next=0;
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
cout<<bfs(x,y)<<endl;
}
return 0;
}
```
**求调咩~**