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

Dungeon Master (luo 三维 BFS )

时间:2019-09-21 10:45:54      阅读:86      评论:0      收藏:0      [点我收藏+]

标签:namespace   mit   typedef   generate   eof   cst   mod   nsis   class   

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!



分析:三维数组+BFS,luo 跑一遍就行
第一份代码是做标记,第二份是改变位置的性质(从可走变为不可走




#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define ull unsigned long long
#define lli long long
#define pq priority_queue<int>
#define pql priority_queue<ll>
#define pqn priority_queue<node>
#define v vector<int>
#define vl vector<ll>
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define NO printf("NO\n");
#define gcd __gcd
#define out(x) cout<<x<<endl;
#define over cout<<endl;
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++)  {scanf("%d",&a[i]) ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define ok return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod(x) ((x)%9973)
#define test cout<<"     ++++++      "<<endl;
//二叉树
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r
//线段树
#define ls now<<1
#define rs now<<1|1
int dir[6][3] = {0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};
//int dir[4][2] = {1,0,-1,0,0,1,0,-1}; //单位移动
//int dir[8][2] = {2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2};
int t,n,m,k,x,y,col,ex,ey,ans,cnt,ly;
int a[202][202],b[202][202];
int vis[35][35][35];
char dp[35][35][35];
typedef struct node
{
    int lv,x,y,z,t;
} node;
node no,now,next;

queue<node>q;
void BFS()
{
    q.push(no);
    dp[no.z][no.x][next.y]=#;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        //cout<<now.t<<endl;
        if(now.x==ex && now.y==ey && now.z==ly)
        {
            cnt = now.t;
            return ;
        }
        for(int i=0; i<6; i++)
        {
            next.z = now.z+dir[i][0];
            next.x = now.x+dir[i][1];
            next.y = now.y+dir[i][2];
            if(next.x<0 || next.x>=m || next.y<0 || next.y>=k || next.z<0 || next.z>=n) continue ;
            if(dp[next.z][next.x][next.y]!=# && !vis[next.z][next.x][next.y])
            {
                vis[next.z][next.x][next.y] = 1;
                next.t=now.t+1;
                q.push(next);
            }
        }
    }
}
int main()
{
    TLE;
    while(cin>>n>>m>>k)
    {
        while(!q.empty()) q.pop();
        if(!n&&!m&&!k) break;
        mem(vis,0);
        cnt=-1;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                for(int kk=0; kk<k; kk++)
                {
                    cin>>dp[i][j][kk];

                    if(dp[i][j][kk]==S)
                        no.z=i,no.x=j,no.y=kk,no.t=0,vis[i][j][kk]=1;
                    if(dp[i][j][kk]==E)
                        ly=i,ex=j,ey=kk;
                }

        BFS();
        if(cnt==-1)
            cout<<"Trapped!"<<endl;
        else
            cout<<"Escaped in "<<cnt<<" minute(s)."<<endl;
    }
    ok;
}

 

 

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
#define ull unsigned long long
#define lli long long
#define pq priority_queue<int>
#define pql priority_queue<ll>
#define pqn priority_queue<node>
#define v vector<int>
#define vl vector<ll>
#define read(x) scanf("%d",&x)
#define lread(x) scanf("%lld",&x);
#define pt(x) printf("%d\n",(x))
#define yes printf("YES\n");
#define NO printf("NO\n");
#define gcd __gcd
#define out(x) cout<<x<<endl;
#define over cout<<endl;
#define rep(j,k) for (int i = (int)(j); i <= (int)(k); i++)
#define input(k) for (int i = 1; i <= (int)(k); i++)  {scanf("%d",&a[i]) ; }
#define mem(s,t) memset(s,t,sizeof(s))
#define ok return 0;
#define TLE std::ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define mod(x) ((x)%9973)
#define test cout<<"     ++++++      "<<endl;
//二叉树
#define lson rt<<1, l, m
#define rson rt<<1|1, m+1, r
//线段树
#define ls now<<1
#define rs now<<1|1
int dir[6][3] = {0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};
//int dir[4][2] = {1,0,-1,0,0,1,0,-1}; //单位移动
//int dir[8][2] = {2,1,2,-1,-2,1,-2,-1,1,2,1,-2,-1,2,-1,-2};
int t,n,m,k,x,y,col,ex,ey,ans,cnt,ly;
int a[202][202],b[202][202];
int vis[35][35][35];
char dp[35][35][35];
typedef struct node
{
    int lv,x,y,z,t;
} node;
node no,now,next;
/*
void DFS(int r,int c)
{
    if(ans>=n*m) {cnt++;return ;}
    for(int i=0;i<8;i++)
    {
        int nx = r + dir[i][0];
        int ny = c + dir[i][1];
        if(nx<0 || ny<0 ||nx >=n ||ny >=m) continue;
        if(!vis[nx][ny])
        {
            vis[nx][ny] = 1;
            ans++;
            DFS(nx,ny);
            ans--;
            vis[nx][ny] = 0;
        }
    }
}
*/
queue<node>q;
void BFS()
{
    q.push(no);
    dp[no.z][no.x][next.y]=#;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        //cout<<now.t<<endl;
        if(now.x==ex && now.y==ey && now.z==ly)
        {
            cnt = now.t;
            return ;
        }
        for(int i=0; i<6; i++)
        {
            next.z = now.z+dir[i][0];
            next.x = now.x+dir[i][1];
            next.y = now.y+dir[i][2];
            if(next.x<0 || next.x>=m || next.y<0 || next.y>=k || next.z<0 || next.z>=n) continue ;
            if(dp[next.z][next.x][next.y]!=#)
            {
                dp[next.z][next.x][next.y]=#;
                next.t=now.t+1;
                q.push(next);
            }
        }
    }
}
int main()
{
    TLE;
    while(cin>>n>>m>>k)
    {
        while(!q.empty()) q.pop();
        if(!n&&!m&&!k) break;
        mem(vis,0);
        cnt=-1;
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                for(int kk=0; kk<k; kk++)
                {
                    cin>>dp[i][j][kk];

                    if(dp[i][j][kk]==S)
                        no.z=i,no.x=j,no.y=kk,no.t=0;
                    if(dp[i][j][kk]==E)
                        ly=i,ex=j,ey=kk;
                }

        BFS();
        if(cnt==-1)
            cout<<"Trapped!"<<endl;
        else
            cout<<"Escaped in "<<cnt<<" minute(s)."<<endl;
    }
    ok;
}

 

Dungeon Master (luo 三维 BFS )

标签:namespace   mit   typedef   generate   eof   cst   mod   nsis   class   

原文地址:https://www.cnblogs.com/Shallow-dream/p/11561511.html

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