1. #include<iostream>
  2. #include<queue>
  3. #include<cstring>
  4. using namespace std;
  5. int x,y,z;
  6. char maze[25][25][25];
  7. bool vis[25][25][25];
  8. int dir[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,-1,0},{0,1,0}};
  9. bool in(int x1,int y1,int z1){
  10. return x1>=0&&x1<x&&y1>=0&&y1<y&&z1>=0&&z1<z;
  11. }
  12. struct node{
  13. int x,y,z,d;
  14. node(int xx,int yy,int zz,int dd){
  15. x=xx;
  16. y=yy;
  17. z=zz;
  18. d=dd;
  19. }
  20. };
  21. int bfs(int sx,int sy,int sz){
  22. queue<node> q;
  23. q.push(node(sx,sy,sz,0));
  24. vis[sx][sy][sz]=true;
  25. while(!q.empty()){
  26. node now=q.front();
  27. q.pop();
  28. for(int i=0;i<6;i++){
  29. int tx=now.x+dir[i][0];
  30. int ty=now.y+dir[i][1];
  31. int tz=now.z+dir[i][2];
  32. if(in(tx,ty,tz)&&maze[tx][ty][tz]!='#'&&!vis[tx][ty][tz]){
  33. if(maze[tx][ty][tz]=='E'){
  34. return now.d+1;
  35. }else{
  36. vis[tx][ty][tz]=true;
  37. q.push(node(tx,ty,tz,now.d+1));
  38. }
  39. }
  40. }
  41. }
  42. return -1;
  43. }
  44. int main()
  45. {
  46. cin>>z>>x>>y;
  47. int ix,iy,iz,ans;
  48. while(x!=0&&y!=0&&z!=0){
  49. memset(maze,0,sizeof(maze));
  50. memset(vis,false,sizeof(vis));
  51. for(int z1=0;z1<z;z1++){
  52. for(int x1=0;x1<x;x1++){
  53. for(int y1=0;y1<y;y1++){
  54. cin>>maze[x1][y1][z1];
  55. if(maze[x1][y1][z1]=='S'){
  56. ix=x1;
  57. iy=y1;
  58. iz=z1;
  59. }
  60. }
  61. }
  62. }
  63. ans=bfs(ix,iy,iz);
  64. if(ans!=-1){
  65. cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
  66. }else{
  67. cout<<"Trapped!"<<endl;
  68. }
  69. cin>>z>>x>>y;
  70. }
  71. return 0;
  72. }
点赞(0)
 

9.9 分

1 人评分

 

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 0 条评论

暂无评论