shiyilang0910 @ 2024-11-27 20:58:56
=
#include<bits/stdc++.h>
using namespace std;
char a[1005][1005];
bool vis[1005][1005];
int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1},n,m;
int ans,maxn;
void dfs(int x,int y,char zhi,int step){
if (zhi=='1'){
zhi='0';
}else{
zhi='1';
}
ans=max(ans,step);
for(int i=0;i<4;i++){
int xx=x+dx[i];
int yy=y+dy[i];
if (xx>=1&&xx<=n&&yy>=1&&yy<=n&&!vis[xx][yy]&&a[xx][yy]==zhi){
vis[xx][yy]=1;
dfs(xx,yy,zhi,step+1);
}
}
return ;
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cin>>a[i][j];
}
}
for(int i=1;i<=m;i++){
int x,y;
cin>>x>>y;
ans=1;
memset(vis,0,sizeof(vis));
vis[x][y]=1;
dfs(x,y,a[x][y],1);
cout<<ans<<endl;
}
return 0;
}
by Cultural_Revolution @ 2024-12-03 17:33:37
处理完每个点再查询