0pts求助
查看原帖
0pts求助
394167
Cure_Wing楼主2021/8/23 22:31

相似题https://www.luogu.com.cn/problem/P2360同思路代码,求为何全 WA。

#include<bits/stdc++.h>
using namespace std;
int l,m,n,ans,sx,sy,sz,head,tail;
char s[50][50][50];
int fx[]={0,-1,1,0,0,0,0},
	fy[]={0,0,0,-1,1,0,0},
	fz[]={0,0,0,0,0,-1,1};
char s1[]="Escaped in %d minute(s).\n",s2[]="Trapped!";
int px[1000000],py[1000000],pz[1000000],foot[1000000];
void bfs(){
	ans=2147483647;
	bool flag[50][50][50]={0};
	head=0,tail=1;
	px[tail]=sx,py[tail]=sy,pz[tail]=sz;flag[sx][sy][sz]=1;
	while(head!=tail){
		++head;
		for(int i=1;i<=6;++i){
			int cx=px[head]+fx[i],
				cy=py[head]+fy[i],
				cz=pz[head]+fz[i];
			if(1<=cx&&cx<=l&&1<=cy&&cy<=m&&0<=cz&&cz<=n-1&&s[cx][cy][cz]!='#'&&!flag[cx][cy][cz]){
				if(s[cx][cy][cz]=='E'){
					ans=min(foot[tail],ans);
					continue;
				}
				px[++tail]=cx,py[tail]=cy,pz[tail]=cz;
				foot[tail]=foot[head]+1;
				flag[cx][cy][cz]=1;
			}
		}
	} 
}
int main(){
	scanf("%d%d%d",&l,&m,&n);
	while(l||m||n){
		for(int h=1;h<=l;++h)
			for(int i=1;i<=m;++i)
				scanf("%s",s[h][i]);
		for(int h=1;h<=l;++h)
			for(int i=1;i<=m;++i)
				for(int j=0;j<=n-1;++j)
					if(s[h][i][j]=='S'){
						sx=h,sy=i,sz=j;
						bfs();
						break;
					}
		if(ans==2147483647) puts(s2);
		else printf(s1,ans);		
	}
	return 0;
}
2021/8/23 22:31
加载中...