• POJ 2251 Dungeon Master


    注:本人英语很渣,题目大意大多来自百度~=0=
     
    题目大意:这题是一个三维的迷宫题目,其中用'.'表示空地,'#'表示障碍物,'S'表示起点,'E'表示终点,求从起点到终点的最小移动次数.
     
    对于题目给出数据的含义就是输入l,r,c,分别代表迷宫有l层,每层长宽分别是c,r。
     
    对于数据以可以这样移动
    (1,1,1)->(1,1,2)->(1,1,3)->(1,1,4)->(1,1,5)->(1,2,5)
    ->(1,3,5)->(1,3,4)->(1,4,4)->(2,4,4)->(2,4,5)->(3,4,,5)
    共11步就可以到达终点
    对于数据二明显不能到达,则输出Trapped
     
    简单BFS  下面是代码
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <queue>
     5 #include <cstdlib>
     6 #include <cmath>
     7 #include <cctype>
     8 using namespace std;
     9  
    10 bool maps[32][32][32];//maps用来记录地图可不可以走
    11 int dir[6][3] = {0,1,0, 1,0,0, 0,-1,0, -1,0,0, 0,0,1, 0,0,-1};//方向
    12 int L, R, C;
    13  
    14 struct node
    15 {
    16     int i, j, k;
    17     int step;//step用来保存步数
    18 } s, e; //s用来保存起点,e用来保存终点
    19  
    20 void BFS()
    21 {
    22     int x, y, z, i;
    23     struct node ss;
    24     queue<node>q;
    25     q.push(s);
    26  
    27     while(!q.empty())
    28     {
    29         ss = q.front();
    30         q.pop();
    31         if(ss.i == e.i && ss.j == e.j && ss.k == e.k)//找到终点,输出步数
    32         {
    33             printf("Escaped in %d minute(s).\n", ss.step);
    34             return;
    35         }
    36         for(i = 0; i < 6; i++)
    37         {
    38             x = ss.i + dir[i][0];
    39             y = ss.j + dir[i][1];
    40             z = ss.k + dir[i][2];
    41             if(x >= 0 && x < L && y >= 0 && y < R && z >= 0 && z < C && maps[x][y][z])
    42             {
    43                 struct node v;
    44                 maps[x][y][z] = false;//走过点标记, 防止重复搜索
    45                 v = {x, y, z, ss.step + 1};
    46                 q.push(v);
    47             }
    48         }
    49     }
    50    
    51     printf("Trapped!\n");//全部搜索完毕,并不能达到终点
    52 }
    53  
    54 int main()
    55 {
    56  
    57     char ch;
    58  
    59     while(scanf("%d %d %d", &L, &R, &C), L || R || C)
    60     {
    61         memset(maps, false, sizeof(maps));
    62  
    63         for(int i = 0; i < L; i++)
    64         {
    65             for(int j = 0; j < R; j++)
    66             {
    67                 scanf(" ");
    68                 for(int k = 0; k < C; k++)
    69                 {
    70                     scanf("%c", &ch);
    71                     if(ch == '.') maps[i][j][k] = true;
    72                     if(ch == 'S') s = {i, j, k, 0};
    73                     if(ch == 'E') e = {i, j, k, 0}, maps[i][j][k] = true;
    74                 }
    75             }
    76         }
    77         BFS();
    78     }
    79    
    80     return 0;
    81 }

     

  • 相关阅读:
    torchline:让Pytorch使用的更加顺滑
    论文笔记系列-AutoFPN
    Latex: 添加IEEE会议论文作者信息
    Latex citation using natbib and footnotesize
    解决 Boost安装:fatal error: bzlib.h: No such file or directory 问题
    将 Graphviz .dot 文件转换为其他格式的图像
    Mac环境下扩容 .vmdk 镜像容量
    解决 dpkg: warning: files list file for package 'x' missing 问题
    Latex 左右引号
    Latex 三线表及设置列数
  • 原文地址:https://www.cnblogs.com/wangyuhao/p/4662089.html
Copyright © 2020-2023  润新知