zoukankan      html  css  js  c++  java
  • Dungeon Master 和上题基本相同 bfs(),求最优值 不同的是三维bfs(),存储有技巧 (u=x*r*c+y*c+z);

    Problem Description
    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 Input
    3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E

     **************************************************************************************************************************

    bfs()  存储有技巧

    ***************************************************************************************************************************

     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<cstdio>
     6 #include<queue>
     7 using namespace std;
     8 int dx[6]={1,-1,0,0,0,0};
     9 int dy[6]={0,0,0,0,1,-1};
    10 int dz[6]={0,0,1,-1,0,0};
    11 int maze[101][101][101];
    12 int vis[101][101][101];
    13 int dis[101][101][101];
    14 int l,r,c,i,j,k;
    15 int que[100001];
    16 int stx,sty,stz;
    17 int bfs(int xt,int yt,int zt)
    18 {
    19     int rear,front;
    20     rear=front=0;
    21     int u=xt*r*c+yt*c+zt;//经典,使数存的更简洁
    22     que[rear++]=u;
    23     vis[xt][yt][zt]=1;
    24     while(front<rear)
    25     {
    26         int su=que[front];
    27         front++;
    28         int x=su/(r*c);
    29         int y=(su%(r*c))/c;
    30         int z=su%c;
    31         for(int it=0;it<6;it++)
    32         {
    33             int xa=x+dx[it];
    34             int ya=y+dy[it];
    35             int za=z+dz[it];
    36             if(xa>=0&&xa<l&&ya>=0&&ya<r&&za>=0&&za<c&&maze[xa][ya][za]!=0&&!vis[xa][ya][za])
    37             {
    38                 u=xa*r*c+ya*c+za;
    39                 que[rear++]=u;
    40                 dis[xa][ya][za]=dis[x][y][z]+1;
    41                 vis[xa][ya][za]=1;
    42                 if(maze[xa][ya][za]==2)
    43                  return  dis[xa][ya][za];
    44             }
    45         }
    46     }
    47     return 0;
    48 }
    49 int main()
    50 {
    51     char  ch;
    52     while(scanf("%d%d%d",&l,&r,&c)&&l&&r&&c)
    53     {
    54         getchar();
    55         memset(vis,0,sizeof(vis));
    56         memset(dis,0,sizeof(dis));
    57         memset(maze,0,sizeof(maze));
    58         for(i=0;i<l;i++)
    59         {
    60             for(j=0;j<r;j++)
    61             {
    62                 for(k=0;k<c;k++)
    63                 {
    64                     scanf("%c",&ch);
    65                     if(ch=='#')maze[i][j][k]=0;
    66                     if(ch=='.')maze[i][j][k]=1;
    67                     if(ch=='E')maze[i][j][k]=2;
    68                     if(ch=='S')
    69                     {
    70                         stx=i;
    71                         sty=j;
    72                         stz=k;
    73                     }
    74                 }
    75                 getchar();
    76             }
    77             getchar();
    78         }
    79     int ans=bfs(stx,sty,stz);
    80     if(ans==0)
    81       printf("Trapped!
    ");
    82     else
    83       printf("Escaped in %d minute(s).
    ",ans);
    84     }
    85     return 0;
    86 
    87 }
    View Code

    坚持!!!!!!!!!!!

  • 相关阅读:
    ES6展开运算符的10个用法
    用react脚手架新建项目
    第六章 组件 56 组件-组件中的data
    第六章 组件 55 组件-使用components定义私有组件
    第六章 组件 54 组件-创建组件的方式3
    第六章 组件 53 组件-创建组件的方式2
    第六章 组件 52 组件-创建组件的方式1
    第六章 组件 51 组件化和模块化的区别以及组件的定义方式
    第五章 动画 50 动画-transition-group中appear和tag属性的作用
    第五章 动画 49 动画-实现列表删除和删除时候的动画效果
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3369267.html
Copyright © 2011-2022 走看看