只能过样例救
查看原帖
只能过样例救
600112
_HHJ楼主2022/8/7 17:31
#include <iostream>
#include <queue>
#include <cstring>

using namespace std;

const int N = 100;
const int fx[] = {1,-1,0,0,0,0};
const int fy[] = {0,0,1,-1,0,0};
const int fz[] = {0,0,0,0,1,-1};
bool g[N][N][N];
int dst[N][N][N];
int h,l,w;
struct node{
	int z,x,y,t;
}ed;


bool check(int z,int x,int y)
{
	return x >= 1 && x <= l && y >= 1 && y <= w
	&&  z >= 1 && z <= h && g[z][x][y];
}

int main(){
	while(cin >> h >> l >> w &&(h&&l&&w))
	{
		queue<node>q;
		memset(dst,0x3f,sizeof dst);
		for(int i = 1 ; i <= h ; i ++ )
			for(int j = 1 ; j <= l ; j ++ )
					for(int k = 1 ; k <= w ; k ++ )
					{
						char ch = getchar();
						if(ch != 'S'&&ch!='E'&&ch!='#'&&ch!='.'){k--;continue;}//不合法重新判断
						if(ch == 'S'){q.push({i,j,k,0});dst[i][j][k] = 0;continue;};//直接入队
						if(ch == 'E'){ed=(node){i,j,k,0};g[i][j][k] = 1;continue;};
						g[i][j][k] = ch == '.';
					}
		bool tot = 1; 
		while(q.size())
		{
			auto zb = q.front();q.pop();
			for(int i = 0 ; i < 6 ; i ++)
			{
				int x = zb.x + fx[i],y = zb.y + fy[i],z = zb.z + fz[i];
				if(check(z,x,y))
				{
					if(z == ed.z && y == ed.y && x == ed.x)
					{
						printf("Escaped in %d minute(s).\n",zb.t+1);
						tot = 0;
						break;
					}
					dst[z][x][y] = min(dst[zb.z][zb.x][zb.y] + 1,dst[z][x][y]);
					g[z][x][y] = 0;
					q.push({z,x,y,dst[z][x][y]});
					
				}
			}
		}
		if(tot)puts("Trapped!");
	}
}
2022/8/7 17:31
加载中...