zoukankan      html  css  js  c++  java
  • 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 }

     

  • 相关阅读:
    JS BOM对象 History对象 Location对象
    JS 字符串对象 数组对象 函数对象 函数作用域
    JS 引入方式 基本数据类型 运算符 控制语句 循环 异常
    Pycharm Html CSS JS 快捷方式创建元素
    CSS 内外边距 float positio属性
    CSS 颜色 字体 背景 文本 边框 列表 display属性
    【Android】RxJava的使用(三)转换——map、flatMap
    【Android】RxJava的使用(二)Action
    【Android】RxJava的使用(一)基本用法
    【Android】Retrofit 2.0 的使用
  • 原文地址:https://www.cnblogs.com/wangyuhao/p/4662089.html
Copyright © 2011-2022 走看看