POJ 2251-Dungeon Master

来源:互联网 发布:java infinity 编辑:程序博客网 时间:2024/05/18 00:18
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18110 Accepted: 7032

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!
小结:
  
  说实话,C++功能的确十分强大,可以极大地缩短代码长度以及增加代码的可读性,不说废话了,说说题目吧,POJ题目分类这道题目是DFS,我就用DFS做了一遍,一直是TLE,学长说要用减枝,真心不会,没办法,搜了搜别人怎么解的,结果貌似都是用BFS解答的,于是暂时放弃DFS做法,不过还是先贴出来,以便于以后学会剪枝算法后再来改进。
 
  ACM真的是一个积累的过程,感觉就像是所有同龄人进行的一场博弈,蛮有趣的,现在至少是这么想的。
AC-BFS代码:
#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>int map[50][50][50],vis[50][50][50];struct node{    int x,y,z;    int step;}point[100000];int dirx[6]={1,0,0,-1,0,0};int diry[6]={0,1,0,0,-1,0};int dirz[6]={0,0,1,0,0,-1};int level,row,col;int sx,sy,sz;int ex,ey,ez;int ok(int a,int b,int c){    if(a<1||b<1||c<1||a>level||b>row||c>col||map[a][b][c]||vis[a][b][c])    return 0;    return 1;}int bfs(){    struct node t;    int top=0,end=0;    t.x=sx;    t.y=sy;    t.z=sz;    t.step=0;    point[top]=t;    vis[sx][sy][sz]=1;    while(top>=end)    {        t=point[end];        end++;        for(int i=0;i<6;i++)        {            struct node temp;            temp=t;            temp.x+=dirx[i];            temp.y+=diry[i];            temp.z+=dirz[i];            temp.step++;            if(temp.x==ex&&temp.y==ey&&temp.z==ez)            {                //printf("aaaaaaaaaaa\n");                return temp.step;            }            if(!ok(temp.x,temp.y,temp.z))            continue;            else            {                vis[temp.x][temp.y][temp.z]=1;                point[++top]=temp;            }        }    }    return -1;}int main(){    char str[50];    while(~scanf("%d%d%d",&level,&row,&col)&&(level&&row&&col))    {        memset(map,0,sizeof(map));        memset(vis,0,sizeof(vis));        for(int i=1;i<=level;i++)        {            for(int j=1;j<=row;j++)            {                scanf("%s",str+1);                for(int k=1;k<=col;k++)                {                    if(str[k]=='S')                    {                        sx=i;                        sy=j;                        sz=k;                    }                    else if(str[k]=='E')                    {                        ex=i;                        ey=j;                        ez=k;                    }                    else if(str[k]=='#')                    map[i][j][k]=1;                }            }        }        int n=bfs();        if(n==-1)        printf("Trapped!\n");        else        printf("Escaped in %d minute(s).\n",n);    }    return 0;}
TLE-DFS代码:
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<math.h>int sx,sy,sz;int level,row,col;//level-层数,row-行,col-列int fans;//最后输出结果int flag;//记录是否有解char map[40][40][40];int ans=0;//过程中累计的步数int dir[6][3]={1,0,0,0,1,0,0,0,1,-1,0,0,0,-1,0,0,0,-1};//坐标改变int ok(int a,int b,int c){    if(a<0||b<0||c<0||a>=level||b>=row||c>=col||map[a][b][c]=='#')    return 0;    return 1;}void dfs(int a,int b,int c){    if(ans>fans)    return;    if(map[a][b][c]=='E')    {        //printf("%d\n",ans);        fans=ans<fans?ans:fans;        flag=1;        return;    }//寻找到目标之后进行步数比较,取小的输出    for(int i=0;i<6;i++)    {        int aa,bb,cc;        aa=a+dir[i][0];        bb=b+dir[i][1];        cc=c+dir[i][2];        if(ok(aa,bb,cc))        {            ans++;            map[a][b][c]='#';            //printf("a\n");            dfs(aa,bb,cc);            map[a][b][c]='.';            ans--;        }    }    return;}int main(){    while(~scanf("%d%d%d",&level,&row,&col))    {        if(!level||!row||!col)        break;        ans=0;        fans=99999999;        flag=0;        for(int i=0;i<level;i++)        {            for(int j=0;j<row;j++)            {                scanf("%s",map[i][j]);            }        }        for(int i=0;i<level;i++)        {            for(int j=0;j<row;j++)            {                for(int k=0;k<col;k++)                {                    if(map[i][j][k]=='S')                    {                        sz=i;                        sx=j;                        sy=k;                    }                }            }        }        dfs(sz,sx,sy);        if(flag==0)            printf("Trapped!\n");        else            printf("Escaped in %d minute(s).\n", fans);    }    return 0;}
0 0
原创粉丝点击