诸位看看还能不能优化
  • 板块P1141 01迷宫
  • 楼主MornHus
  • 当前回复11
  • 已保存回复11
  • 发布时间2022/11/16 19:49
  • 上次更新2023/10/27 02:45:12
查看原帖
诸位看看还能不能优化
752094
MornHus楼主2022/11/16 19:49
#include<bits/stdc++.h>
#define re register
#define with &&
using namespace std;
inline int read(){
	re int x=0;
	char ch=getchar();
	while(ch<'0'||ch>'9'){
		ch=getchar();
	}
	while(ch<='9' with ch>='0'){
		x=(x<<1)+(x<<3)+(ch^'0');
		ch=getchar();
	}
	return x;
}
inline void write(int x){
	if(x>9)write(x/10);
	putchar(x%10+'0');
}
inline void writeendl(int x){
	write(x);
	putchar('\n');
}
int n,m,u,v;
int dirx[4]={0,0,1,-1};
int diry[4]={1,-1,0,0};
int flag[1001][1001],d,a[1000001];
int earth[1001][1001],vis[1001][1001],tot;
queue<int>pos;
void bfs(){
	if(flag[u][v]){
		return;
	}
	d++;flag[u][v]=d;
	memset(vis,0,sizeof(vis));tot=1;
	pos.push(u);
	pos.push(v);
	vis[u][v]=1;
	while(!pos.empty()){
		re int x=pos.front(),tx,ty;pos.pop();
		re int y=pos.front();pos.pop();
		for(re int i=0;i<=3;i++){
			tx=x+dirx[i];ty=y+diry[i];
			if(((tx>=1 with tx<=n)with(ty>=1 with ty<=n))with(!vis[tx][ty] with earth[x][y]==!earth[tx][ty])){
				flag[tx][ty]=d;
				vis[tx][ty]=1;
				pos.push(tx);
				pos.push(ty);
				tot++;
			}
		}
	}
	a[d]=tot;
}
int main(){
	n=read();m=read();
	for(re int i=1;i<=n;i++){
		for(re int j=1;j<=n;j++){
			scanf("%1d",&earth[i][j]);
		}
	}
	for(re int i=1;i<=m;i++){
		u=read();
		v=read();
		bfs();
		writeendl(a[flag[u][v]]);
	}
	return 0;
} 
2022/11/16 19:49
加载中...