Dungeon Master bfs

本文介绍了一种使用广度优先搜索算法解决三维迷宫问题的方法。通过定义迷宫结构、状态转移以及边界条件,实现了一个可以计算从起点到终点最短路径的程序。并详细解释了如何利用队列进行状态拓展。
time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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

1 3 3
S##
#E#
###

0 0 0

Sample Output

Escaped in 11 minute(s).
Trapped!

queue用法

queue 模板类的定义在<queue>头文件中。
queue 模板类需要两个模板参数,一个是元素类型,一个容器类
型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
定义queue 对象的示例代码如下:
queue<int> q1;
queue<double> q2;

queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()

 

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<queue>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     queue<int> q;
 9     q.push(1);
10     q.push(3);
11     q.push(5);
12     printf("%d\n",q.size());
13     while(!q.empty())
14     {
15         int a=q.front();
16         q.pop();
17         printf("%d ",a);
18     }
19     printf("\n");
20     return 0;
21 }
View Code

 

my daima

  1 #include<stdio.h>
  2 #include<string.h>
  3 #include<queue>
  4 using namespace std;
  5 
  6 const int INF=9999999;
  7 int l,r,c,sl,sr,sc,el,er,ec,d[35][35][35];
  8 char maps[35][35][35];
  9 
 10 struct Node
 11 {
 12     int l;
 13     int r;
 14     int c;
 15 };
 16 
 17 queue<Node> q;
 18 
 19 int dr[6]={0,0,1,-1,0,0},dc[6]={0,0,0,0,1,-1},dl[6]={1,-1,0,0,0,0};
 20 
 21 int bfs()
 22 {
 23     int i,j,k;
 24     for(k=1;k<=l;k++)
 25         for(i=1;i<=r;i++)
 26             for(j=1;j<=c;j++)
 27                 d[k][i][j]=INF;
 28     Node s;
 29     s.l=sl,s.r=sr,s.c=sc;
 30     q.push(s);
 31     d[sl][sr][sc]=0;
 32 
 33     while(q.size())
 34     {
 35         Node now=q.front();
 36         q.pop();
 37         Node nex;
 38 
 39         if(now.l==el && now.r==er && now.c==ec)
 40             break;
 41 
 42         for(i=0;i<6;i++)
 43         {
 44             nex.l=now.l+dl[i],nex.r=now.r+dr[i],nex.c=now.c+dc[i];
 45 
 46             if(1<=nex.l && nex.l<=l && 1<=nex.r && nex.r<=r && 1<=nex.c && nex.c<=c && maps[nex.l][nex.r][nex.c]!='#' && d[nex.l][nex.r][nex.c]==INF)
 47             {
 48                 q.push(nex);
 49                 d[nex.l][nex.r][nex.c]=d[now.l][now.r][now.c]+1;
 50             }
 51         }
 52     }
 53     return d[el][er][ec];
 54 }
 55 
 56 int main()
 57 {
 58     int i,j,k;
 59     while(scanf("%d %d %d",&l,&r,&c)!=EOF)
 60     {
 61         getchar();
 62         if(l==0 && r==0 && c==0)
 63             break;
 64         for(k=1;k<=l;k++)
 65         {
 66             for(i=1;i<=r;i++)
 67             {
 68                 for(j=1;j<=c;j++)
 69                 {
 70                     scanf("%c",&maps[k][i][j]);
 71                     if(maps[k][i][j]=='S')
 72                         sl=k,sr=i,sc=j;
 73                     if(maps[k][i][j]=='E')
 74                         el=k,er=i,ec=j;
 75                 }
 76                 getchar();
 77             }
 78             getchar();
 79         }
 80 
 81         int ans=bfs();
 82         if(ans==INF)
 83             printf("Trapped!\n");
 84         else
 85             printf("Escaped in %d minute(s).\n",ans);
 86 
 87 
 88         /*printf("%d %d %d\n%d %d %d\n",sl,sr,sc,el,er,ec);
 89         for(k=1;k<=l;k++)
 90         {
 91             for(i=1;i<=r;i++)
 92             {
 93                 for(j=1;j<=c;j++)
 94                 {
 95                     printf("%7d ",d[k][i][j]);
 96                 }
 97                 printf("\n");
 98             }
 99             printf("\n");
100         }*/
101 
102     }
103     return 0;
104 }
View Code

 

dsdm

#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>
#include <queue>

using namespace std;

int l, r, c, res;
int sx, sy, sz, ex, ey, ez;
string m[31][31];
bool vis[31][31][31], esp;

struct Node {int x, y, z, rs;};
int dir[6][3] = {-1,0,0,1,0,0,0,-1,0,0,1,0,0,0,-1,0,0,1};
queue<Node> q;

void bfs() {
    Node s;
    s.x = sx, s.y = sy, s.z = sz, s.rs = 0;
    while(!q.empty()) q.pop();
    q.push(s);
    while(!q.empty()) {
        Node now = q.front(); q.pop();
        Node tmp;
        for (int i = 0; i < 6; ++i) {
            int x = now.x+dir[i][0];
            int y = now.y+dir[i][1];
            int z = now.z+dir[i][2];
            if (x >= l || x < 0 || y >= r || y < 0 || z >= c || z < 0) continue;
            if (vis[z][x][y]) continue;
            if (m[z][x][y] =='#') continue;
            vis[z][x][y] = true;
            tmp.x = x;
            tmp.y = y;
            tmp.z = z;
            tmp.rs = now.rs +1;
            q.push(tmp);
            if (x == ex && y == ey && z == ez) {
                esp = true;
                res = tmp.rs;
                return ;
            }
        }
    }
    return ;
}

int main() {
    while(cin >> c >>  l >> r) {
        if (!l && !r && !c) break;
        for (int i = 0; i < c; ++i) {
            for (int j = 0; j < l; ++j) {
                cin >> m[i][j];
                for (int k = 0; k < m[i][j].size(); ++k) {
                    if (m[i][j][k] == 'S') {
                        sx = j;
                        sy = k;
                        sz = i;
                    }
                    else if (m[i][j][k] == 'E') {
                        ex = j;
                        ey = k;
                        ez = i;
                    }
                }
            }
        }
        memset(vis, 0, sizeof(vis));
        vis[sz][sx][sy] = true;
        esp = false;
        bfs();
        if (esp) cout << "Escaped in "<<res<< " minute(s)." << endl;
        else cout << "Trapped!" << endl;
    }
    return 0;
}
View Code

 

 

 

转载于:https://www.cnblogs.com/cyd308/p/4490987.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值