zju 1940 Dungeon Master(三维BFS暴搜题)

刚开始

memset(map,'#',sizeof(map));
memset(mins,-1,sizeof(map));初始化错了。导致一直WA,草。。。

不过改过来以后,瞬间0MS跑过。。。哎,自己还是太弱了,

#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

#define MAX_N 35
typedef struct p{
	int x,y,z;
}p;

char map[MAX_N][MAX_N][MAX_N];
int l,n,m,mins[MAX_N][MAX_N][MAX_N];
int step[18]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
p s,e;

void Init(){

	memset(map,'#',sizeof(map));
	memset(mins,-1,sizeof(mins));

	for(int i=1;i<=l;i++){

		for(int j=1;j<=n;j++){

			cin>>(map[i][j]+1);

			for(int k=1;k<=m;k++){

				if(map[i][j][k]=='S')
					s.z=i,s.x=j,s.y=k;
				if(map[i][j][k]=='E')
					e.z=i,e.x=j,e.y=k;
			}
		}
	}
}
void putstruct(p u){
	cout<<"Z : "<<u.z<<" X : "<<u.x<<" Y : "<<u.y<<endl;
}
void bfs(){
	
	queue<p> q;
	q.push(s);
	mins[s.z][s.x][s.y]=0;
	while(!q.empty()){

		p t=q.front();
		q.pop();

		for(int i=0;i<18;i+=3){
			p d;
			d.z=t.z+step[i];
			d.x=t.x+step[i+1];
			d.y=t.y+step[i+2];
		//	putstruct(d);
			if(map[d.z][d.x][d.y]=='#'|| mins[d.z][d.x][d.y]>=0 ) 
				continue;

			mins[d.z][d.x][d.y]=mins[t.z][t.x][t.y]+1;
		//	cout<<mins[d.z][d.x][d.y]<<endl;
			q.push(d);
			if(d.x==e.x && d.y==e.y && d.z==e.z)
				return ;
		}
	}


}
void output(){
	for(int i=1;i<=l;i++,cout<<endl)
		for(int j=1;j<=n;j++,cout<<endl)
			for(int k=1;k<=m;k++)
				cout<<mins[i][j][k]<<" ";
}
void outmap(){
	for(int i=1;i<=l;i++,cout<<endl)
		for(int j=1;j<=n;j++,cout<<endl)
			for(int k=1;k<=m;k++)
				cout<<map[i][j][k]<<" ";
}
int main(){

	while(cin>>l>>n>>m && n && l && m){
		Init();
	//	outmap();
		bfs();
	//	output();
		if(mins[e.z][e.x][e.y]<0)
			cout<<"Trapped!"<<endl;
		else{
	//		if(mins[e.z][e.x][e.y]==1)
		//		cout<<"Escaped in "<<mins[e.z][e.x][e.y]<<" minute."<<endl;
	//		else
				cout<<"Escaped in "<<mins[e.z][e.x][e.y]<<" minute(s)."<<endl;
		}		
	}
	return 0;
}


你可能感兴趣的:(zju 1940 Dungeon Master(三维BFS暴搜题))