90分 #3wa 求助神牛//带有代码注释
查看原帖
90分 #3wa 求助神牛//带有代码注释
590571
_weishiqi66_楼主2023/3/1 13:14
#include<bits/stdc++.h>
#define ll long long
#define yy yyy
#define map mapp
const int N1=100005;
const int N2=1005;
const int wx[5]={0,+1,-1,0,0};
const int wy[5]={0,0,0,+1,-1};
using namespace std;

int n,m,jsq;//jsq表示连通块序号 
int map[N2][N2],ans[N1];//ans表示连通块序号对应的答案; 
bool a[N1][N2];
void ss(int x,int y){
	//if(x<1||y<1||x>n||y>n) return ;
	for(int i=1;i<=4;i++){
		int xx=x+wx[i],yy=y+wy[i];
		if(xx<1||yy<1||xx>n||yy>n||map[xx][yy]||a[xx][yy]==a[x][y])
			continue;
		map[xx][yy]=jsq;
		ans[jsq]++;	
		ss(xx,yy);
	}
	return;
}
int main(){
	//freopen("P1141_3.in","r",stdin);
	//freopen("out.txt","w",stdout);
	std::ios::sync_with_stdio(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			char b;
			cin>>b;
			if(b=='1') a[i][j]=1;
		}		
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(map[i][j]!=0) continue;
			jsq++;
			ss(i,j);
		} 
	}
	int x,y;
	for(ll i=1;i<=m;i++){
		cin>>x>>y;
		cout<<ans[map[x][y]]<<endl;
	}
	return 0;
}
2023/3/1 13:14
加载中...