码迷,mamicode.com
首页 > 其他好文 > 详细

Dungeon Master的两种方法

时间:2018-05-21 19:30:39      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:简单的   NPU   ble   --   temp   code   may   lock   cat   

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!

简单的说就是在三维地图中找最短路,具体题目请参见POJ2251

一、普通的bfs,配合优先队列

代码如下:

  1 #include<stdio.h>
  2 #include<iostream>
  3 #include<queue>
  4 using namespace std;
  5 char map[30][30][30];        //记录节点信息
  6 int sta[30][30][30];        //标记是否访问
  7 int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
  8 int L, R, C;
  9 struct Piont
 10 {
 11     int x, y, z;            //位置坐标
 12     int step;                //出发点到该点的步数
 13 };
 14 struct Piont s;                //起点
 15 struct Piont e;                //终点
 16 struct Piont curp;            //跳出循环时的节点
 17 
 18 /******************判断是否到达终点*********************/
 19 bool success(struct Piont cur)
 20 {
 21     if (cur.x == e.x && cur.y == e.y && cur.z == e.z)
 22         return true;
 23     else
 24         return false;
 25 }
 26 
 27 /**************判断该点是否合法*************************/
 28 bool check(int x, int y, int z)
 29 {
 30     if ((x >= 0) && (x < L) && (y >= 0) && (y < R) && (z >= 0) && (z < C) && (!sta[x][y][z]) && (map[x][y][z] == . || map[x][y][z] == E))
 31         return true;
 32     else
 33         return false;
 34 }
 35 
 36 /*************************深搜***************************/
 37 void bfs()
 38 {
 39     struct Piont next;
 40     queue<Piont>q;
 41     q.push(s);
 42     //int flag = 0;
 43     while (!q.empty())
 44     {
 45         curp = q.front();
 46         q.pop();
 47         if (success(curp))
 48             return;
 49         else
 50         {
 51             sta[curp.x][curp.y][curp.z] = 1;
 52             for (int i = 0; i < 6; i++)
 53             {
 54                 next.x = curp.x + base[i][0];
 55                 next.y = curp.y + base[i][1];
 56                 next.z = curp.z + base[i][2];
 57                 if (check(next.x, next.y, next.z))        //扩展队列
 58                 {
 59                     next.step = curp.step + 1;
 60                     sta[next.x][next.y][next.z] = 1;
 61                     q.push(next);
 62                 }
 63              }
 64         }
 65     }
 66 }
 67 int main()
 68 {
 69     while (scanf("%d%d%d", &L, &R, &C))
 70     {
 71         if((L == 0) && (R == 0) && (C == 0))
 72             break;
 73         memset(sta, 0, sizeof(sta));
 74         for (int i = 0; i < L; i++) {
 75             getchar();
 76             for (int j = 0; j < R; j++) {
 77                 for (int k = 0; k < C; k++)
 78                 {
 79                     scanf("%c", &map[i][j][k]);
 80                     if (map[i][j][k] == S) {
 81                         s.x = i;
 82                         s.y = j;
 83                         s.z = k;
 84                         s.step = 0;
 85                     }
 86                     else if (map[i][j][k] == E)
 87                     {
 88                         e.x = i;
 89                         e.y = j;
 90                         e.z = k;
 91                     }
 92                 }
 93                 getchar();
 94             }
 95         }
 96         bfs();
 97         if (curp.x == e.x && curp.y == e.y && curp.z == e.z)
 98             printf("Escaped in %d minute(s).\n", curp.step);
 99         else
100             printf("Trapped!\n");
101     }
102     return 0;
103 }

二、递归(但由于多次重复经过某点,时间复杂度远大于方法一)

仅供参考,代码如下:

  1 #include<stdio.h>
  2 #include<iostream>
  3 using namespace std;
  4 char map[30][30][30];
  5 int step_map[30][30][30];
  6 int sta[30][30][30];
  7 int s_x = -1, s_y = -1, s_z = -1;
  8 int e_x = -1, e_y = -1, e_z = -1;
  9 int step = 0, minn = 1 << 25;
 10 int L, R, C;
 11 int base[6][3] = { {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };
 12 
 13 bool check(int x, int y, int z)
 14 {
 15     if ((x >= 0) && (x < L) && (y >= 0) && (y < R) && (z >= 0) && (z < C))
 16         return true;
 17     else
 18         return false;
 19 }
 20 void bfs(int x, int y, int z)
 21 {
 22     int temp_x, temp_y, temp_z;
 23     for (int i = 0; i < 6; i++)
 24     {
 25         if (x == e_x + base[i][0] && y == e_y + base[i][1] && z == e_z + base[i][2])
 26         {
 27             if (step < minn)
 28                 minn = step;
 29             return;
 30         }
 31     }
 32     for (int i = 0; i < 6; i++)
 33     {
 34         temp_x = x + base[i][0];
 35         temp_y = y + base[i][1];
 36         temp_z = z + base[i][2];
 37         if ((!sta[temp_x][temp_y][temp_z]) && (map[temp_x][temp_y][temp_z] == .) && (check(temp_x, temp_y, temp_z)))
 38         {
 39             step++;
 40             if (step < step_map[temp_x][temp_y][temp_z])        //剪枝二:当前步数已大于曾经过该点的最小步数,停止搜索
 41             {
 42                 step_map[temp_x][temp_y][temp_z] = step;        
 43                 if (step < minn)                                //剪枝一:当前步数已大于或等于最小步数,停止搜索
 44                 {
 45                     sta[temp_x][temp_y][temp_z] = 1;
 46                     bfs(temp_x, temp_y, temp_z);
 47                     sta[temp_x][temp_y][temp_z] = 0;
 48                 }
 49             }
 50             step--;
 51         }
 52     }
 53 }
 54 int main()
 55 {
 56     while (scanf("%d%d%d",&L,&R,&C))
 57     {
 58         if ((L == 0) && (R == 0) && (C == 0))
 59             break;
 60         memset(sta, 0, sizeof(sta));
 61         //memset(step_map, (1 << 25), sizeof(step_map));//只能用来初始化为0、1和-1
 62         for (int i = 0; i < 30; i++)
 63             for (int j = 0; j < 30; j++)
 64                 for (int k = 0; k < 30; k++)
 65                     step_map[i][j][k] = (1 << 25);
 66         
 67         for (int i = 0; i < L; i++) {
 68             getchar();
 69             for (int j = 0; j < R; j++) {
 70                 for (int k = 0; k < C; k++)
 71                 {
 72                     //cin >> map[i][j][k];
 73                     scanf("%c", &map[i][j][k]);
 74                     if (map[i][j][k] == S) {
 75                         s_x = i;
 76                         s_y = j;
 77                         s_z = k;
 78                     }
 79                     if (map[i][j][k] == E)
 80                     {
 81                         e_x = i;
 82                         e_y = j;
 83                         e_z = k;
 84                     }
 85                 }
 86                 getchar();
 87             }        
 88         }
 89                 
 90         bfs(s_x, s_y, s_z);
 91         if (minn == (1 << 25))
 92             printf("Trapped!");
 93         else
 94         {
 95             printf("Escaped in %d minnute(s).", minn + 1);
 96             minn = (1 << 25);
 97             step = 0;
 98         }
 99     }
100     return 0;
101 }

新手入门,希望大家多多指教!

 

Dungeon Master的两种方法

标签:简单的   NPU   ble   --   temp   code   may   lock   cat   

原文地址:https://www.cnblogs.com/lfri/p/9068564.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!