程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> UVa 532 - Dungeon Master

UVa 532 - Dungeon Master

編輯:C++入門知識

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 Specification
The input file 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 Specification
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就可以
[plain] 
#include <iostream> 
#include <stdio.h> 
#include <string.h> 
using namespace std; 
int x[]={0,0,0,0,-1,1}; 
int y[]={0,0,1,-1,0,0}; 
int z[]={1,-1,0,0,0,0}; 
char s1[40][40][40]; 
int statckx[100000],statcky[100000],statckz[100000]; 
int status[40][40][40]; 
int sum[40][40][40]; 
int L,R,C; 
int main() 

    int bfs(int posx,int posy,int posz); 
    int i,j,n,m,s,t,k; 
    int posx,posy,posz; 
    char cha; 
    char s2[100]; 
    while(cin>>L>>R>>C) 
    { 
        if(L==0&&R==0&&C==0) 
        { 
            break; 
        } 
        for(i=0;i<=L-1;i++) 
        { 
            for(j=0;j<=R-1;j++) 
            { 
                for(k=0;k<=C-1;k++) 
                { 
                    cin>>s1[i][j][k]; 
                    if(s1[i][j][k]=='S') 
                    { 
                        posz=i; 
                        posx=j;  posy=k; 
                    } 
                } 
                getchar(); 
            } 
            gets(s2); 
        } 
        memset(status,0,sizeof(status)); 
        memset(sum,0,sizeof(sum)); 
        t=bfs(posx,posy,posz); 
        if(t==-1) 
        { 
            cout<<"Trapped!"<<endl; 
        }else 
        { 
            cout<<"Escaped in "<<t<<" minute(s)."<<endl; 
        } 
    } 
    return 0; 

int bfs(int posx,int posy,int posz) 

    int top1,top2; 
    int xend,yend,zend,i; 
    top1=top2=0; 
    statckx[top2]=posx; statcky[top2]=posy; 
    statckz[top2++]=posz; 
    status[posz][posx][posy]=1; 
    while(top1<top2) 
    { 
        posx=statckx[top1]; posy=statcky[top1]; posz=statckz[top1++]; 
        for(i=0;i<=5;i++) 
        { 
            xend=posx+x[i]; yend=posy+y[i]; zend=posz+z[i]; 
            if(status[zend][xend][yend]==0&&xend>=0&&xend<=R-1&¥d>=0&¥d<=C-1&&zend>=0&&zend<=L-1&&(s1[zend][xend][yend]=='.'||s1[zend][xend][yend]=='E')) 
            { 
                statckx[top2]=xend; statcky[top2]=yend; 
                statckz[top2++]=zend; 
                status[zend][xend][yend]=1; 
                sum[zend][xend][yend]=sum[posz][posx][posy]+1; 
                if(s1[zend][xend][yend]=='E') 
                { 
                    break; 
                } 
            } 
        } 
        if(i!=6) 
        { 
            return sum[zend][xend][yend]; 
        } 
    } 
    return -1; 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved