• B - Dungeon Master


    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and the maze is surrounded by solid rock on all sides.

    Is an escape possible? If yes, how long will it take?

    Input

    The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
    L is the number of levels making up the dungeon.
    R and C are the number of rows and columns making up the plan of each level.
    Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.

    Output

    Each maze generates one line of output. If it is possible to reach the exit, print a line of the form

    Escaped in x minute(s).


    where x is replaced by the shortest time it takes to escape.
    If it is not possible to escape, print the line

    Trapped!

    Sample

    InputcopyOutputcopy
    3 4 5
    S....
    .###.
    .##..
    ###.#
    
    #####
    #####
    ##.##
    ##...
    
    #####
    #####
    #.###
    ####E
    
    1 3 3
    S##
    #E#
    ###
    
    0 0 0
    
    Escaped in 11 minute(s).
    Trapped!

     人写麻了,

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. typedef long long ll;
    4. #define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    5. int way[6][3]={{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1},};
    6. //六个方向
    7. int l,r,c;
    8. //长宽高
    9. const int maxx=32;
    10. //注意数组范围
    11. char dungeon[maxx][maxx][maxx];
    12. //地牢
    13. bool visited[maxx][maxx][maxx];
    14. //判断是否访问过
    15. typedef struct node{
    16. int x,y,z;
    17. int step;
    18. }node;
    19. //结构体定义坐标,步数
    20. int start_x,start_y,start_z;
    21. //起点
    22. int end_x,end_y,end_z;
    23. //终点
    24. bool check(int x,int y,int z){
    25. if(x<0||x>=l||y<0||y>=r||z<0||z>=c) return true;
    26. //越界情况
    27. else if(visited[x][y][z]) return true;
    28. //是否走过该位置
    29. else if(dungeon[x][y][z]=='#') return true;
    30. //是否有障碍物
    31. return false;
    32. }
    33. int bfs(){
    34. queue<node> q;
    35. node t,head;
    36. //head存队头元素
    37. head.x=start_x;head.y=start_y;head.z=start_z;
    38. head.step=0;
    39. q.push(head);
    40. while(!q.empty()){
    41. head=q.front();
    42. if(head.x==end_x&&head.y==end_y&&head.z==end_z) return head.step;
    43. q.pop();
    44. for(int i=0;i<6;i++){
    45. t.x=head.x+way[i][0];
    46. t.y=head.y+way[i][1];
    47. t.z=head.z+way[i][2];
    48. if(check(t.x,t.y,t.z)) continue;
    49. t.step=head.step+1;
    50. visited[t.x][t.y][t.z]=true;
    51. q.push(t);
    52. }
    53. }
    54. return 0;
    55. }
    56. int main(){
    57. IOS;
    58. while(cin>>l>>r>>c){
    59. if(l==0&&r==0&&c==0) break;
    60. for(int i=0;i<l;i++){
    61. for(int j=0;j<r;j++){
    62. cin>>dungeon[i][j];
    63. for(int k=0;k<c;k++){
    64. if(dungeon[i][j][k]=='S'){
    65. start_x=i;
    66. start_y=j;
    67. start_z=k;
    68. }
    69. else if(dungeon[i][j][k]=='E'){
    70. end_x=i;
    71. end_y=j;
    72. end_z=k;
    73. }
    74. }
    75. }
    76. }
    77. memset(visited,false,sizeof(visited));
    78. int res=bfs();
    79. if(res) cout<<"Escaped in "<<res<<" minute(s)."<<endl;
    80. else cout<<"Trapped!"<<endl;
    81. }
    82. }

  • 相关阅读:
    【老生谈算法】matlab实现高斯白噪声仿真算法源码——高斯白噪声
    SEGGER调试利器RTT,替代串口,高速数据上传
    re:从0开始的CSS之旅 16. 高度塌陷问题
    深度学习中的模型设计
    SCI一区 | Matlab实现POA-TCN-BiGRU-Attention鹈鹕算法优化时间卷积双向门控循环单元注意力机制多变量时间序列预测
    paddlenlp:社交网络中多模态虚假媒体内容核查(特征篇)
    【DW组队学习—动手学数据分析】第二章:第四节数据可视化
    火狐为 Firefox所有用户推出了GPC
    .net8 blazor auto模式很爽(五)读取sqlite并显示(2)
    人工智能之地形导航系统
  • 原文地址:https://blog.csdn.net/LanceLSf/article/details/125632095