0
点赞
收藏
分享

微信扫一扫

Dungeon Master

J简文 2022-11-07 阅读 178


题目:
​​​题目链接​​

题解:
bfs
注意上下楼就好

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
char pos[50][50][50];
int vis[50][50][50];
int to[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
int f,ans;
int x1,y1,z1,x2,y2,z2;
int k,n,m;
struct node {
int x,y,z,step;
};
void bfs()
{
queue<node> q;
while(!q.empty()) q.pop();
struct node now,next;
now.x=x1,now.y=y1,now.z=z1,now.step=0;
vis[z1][x1][y1]=1;
q.push(now);
while(!q.empty())
{
now=q.front();
q.pop();
if(now.x==x2&&now.y==y2&&now.z==z2)
{
f=1;
ans=now.step;
break;
}
for(int i=0;i<6;i++)
{
next.z=now.z+to[i][0];
next.x=now.x+to[i][1];
next.y=now.y+to[i][2];
if(next.z<0||next.z>=k||next.x<0||next.x>=n||next.y<0||next.y>=m||pos[next.z][next.x][next.y]=='#'||vis[next.z][next.x][next.y])
{
continue;
}
next.step=now.step+1;
vis[next.z][next.x][next.y]=1;
q.push(next);
}
}
}
int main()
{
while(cin>>k>>n>>m)
{
if(k==0&&n==0&&m==0)
{
break;
}
f=0;
ans=0;
memset(vis,0,sizeof(vis));
for(int i=0;i<k;i++)
{
for(int j=0;j<n;j++)
{
cin>>pos[i][j];
}
}
for(int i=0;i<k;i++)
{
for(int j=0;j<n;j++)
{
for(int l=0;l<m;l++)
{
if(pos[i][j][l]=='S')
{
z1=i,x1=j,y1=l;
}
if(pos[i][j][l]=='E')
{
z2=i,x2=j,y2=l;
}
}
}
}
bfs();
if(f) cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
else cout<<"Trapped!"<<endl;
}
return 0;
}


举报

相关推荐

0 条评论