跳到正文

个人记录

D4298 Dungeon Master

Joeger约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 2 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
using namespace std;
int x,y,z,vis[33][33][33];
int d[10][5]={{-1,0,0},{1,0,0},{0,-1,0},{0,1,0},
{0,0,1},{0,0,-1}};
char a[33][33][33];
int ax,ay,az,bx,by,bz;
struct node{
	int x,y,z;
};
void bfs()
{
	queue<node>q;
	node w={ax,ay,az};
	q.push(w);
	vis[ax][ay][az]=0;
	while(q.size())
	{
		node t=q.front();
		q.pop();
		for(int i=0;i<6;i++)
		{
			int o=t.x+d[i][0],p=t.y+d[i][1],
			l=t.z+d[i][2];
			if(o<1 || o>x || p<1 || p>y || l<1 || l>z)
			{
				continue;
			}
			if(vis[o][p][l]!=-1)
			{
				continue;
			}
			if(a[o][p][l]=='#')
			{
				continue;
			}
			vis[o][p][l]=vis[t.x][t.y][t.z]+1;
			node w={o,p,l};
			q.push(w);
		}
	}
}
int main()
{
	while(1)
	{
		cin>>x>>y>>z;
		if(x==0 && y==0 && z==0)
		{
			return 0;
		}
		for(int i=1;i<=x;i++)
		{
			for(int j=1;j<=y;j++)
			{
				for(int k=1;k<=z;k++)
				{
					cin>>a[i][j][k];
					vis[i][j][k]=-1;
					if(a[i][j][k]=='S')
					{
						ax=i,
						ay=j,
						az=k;
					 }
					if(a[i][j][k]=='E')
					{
						bx=i,
						by=j,
						bz=k;
					}
				}
			}
		}
		bfs();
		if(vis[bx][by][bz]!=-1)
		{
			cout<<"Escaped in "<<vis[bx][by][bz]<<" minute(s).";
		}
		else
		{
			cout<<"Trapped!";
		}
		cout<<"\n";
	}
	return 0;
}
原文