40分求助大佬 其他点全WA是为什么啊
  • 板块P1141 01迷宫
  • 楼主Guoge66
  • 当前回复0
  • 已保存回复0
  • 发布时间2022/3/11 15:42
  • 上次更新2023/10/28 06:52:12
查看原帖
40分求助大佬 其他点全WA是为什么啊
354511
Guoge66楼主2022/3/11 15:42
#include<bits/stdc++.h>

using namespace std;

const int N=1010;
const int M=100010;

typedef pair<int,int> PII;

PII q[N*N],all[N*N];
char g[N][N];
int d[N][N];
int x[M],y[M];
int step[N][N];
bool occupy[N][N];

int bfs(int x,int y,int n){
	int hh=0,tt=0;
	int cnt=0;
	int nx=0,ny=0;
	q[0]={x,y};
	int dx[4]={-1,1,0,0};
	int dy[4]={0,0,-1,1};

	int flag=0;
	all[flag].first=x;
	all[flag].second=y;
	while(hh<=tt){
		
		auto temp=q[hh++];//队头 
		for(int i=0;i<4;i++){
			nx=dx[i]+temp.first;
			ny=dy[i]+temp.second;
			if(nx>=0&&nx<n&&ny>=0&&ny<n&&g[nx][ny]!=g[temp.first][temp.second]&&!occupy[nx][ny]){
				flag++;
				occupy[nx][ny]=true;
				all[flag].first=nx;
				all[flag].second=ny;
				q[++tt]={nx,ny};
				cnt++;
			}
		}
		
	}
	for(int i=0;i<=flag;i++){
		step[all[i].first][all[i].second]=cnt;
	}
	return cnt;
}

int main(void){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>g[i][j];
		}
	}

	for(int i=0;i<m;i++){
		scanf("%d%d",&x[i],&y[i]);
		if(occupy[x[i]-1][y[i]-1]){
			printf("%d\n",step[x[i]-1][y[i]-1]);
		}
		else{
			bfs(x[i]-1,y[i]-1,n);
			printf("%d\n",step[x[i]-1][y[i]-1]);
		}
	}

	return 0;
} 
2022/3/11 15:42
加载中...