Dungeon Master (BFS)

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!
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<time.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
using namespace std;

#define LL long long
#define mst(a) memset(a,0,sizeof(a))

const int max_n=51;
int my_next[6][3]={{0,0,1},{0,0,-1},{1,0,0},{-1,0,0},{0,1,0},{0,-1,0}};
int vis[max_n][max_n][max_n],a[max_n][max_n][max_n];
int l,r,c,ans;
char ch[max_n];
struct node{
    int x,y,z;
    int step;
};
bool check(node w)
{
    if(w.x<=r && w.y<=c && w.z<=l && w.x>=1 && w.y>=1 && w.z>=1 && a[w.z][w.x][w.y]!=1&&vis[w.z][w.x][w.y]==0)
    return true;
    else return false;
}
int main()
{
  //  freopen("1.in","r",stdin);freopen("1.out","w",stdout);
    while(scanf("%d%d%d",&l,&r,&c)!=EOF)
    {
        mst(vis);
        mst(a);
        ans=-1;
        node b,next,e;
        queue<node>q;
        if(l==0&&r==0&&c==0)
        {
            return 0;
        }
        for(int i=1;i<=l;i++)
        {
            for(int j=1;j<=r;j++)
            {
				scanf("%s",ch);
                for(int k=1;k<=c;k++)
                {
                    if(ch[k-1]=='#')
                    {
                        a[i][j][k]=1;
                    }
                    if(ch[k-1]=='S')
                    {
                        b.z=i,b.x=j,b.y=k;
                        b.step=0;
                    }
                    if(ch[k-1]=='E')
                    {
                        e.z=i,e.x=j,e.y=k;
                    } 
                }
            }
        }
        q.push(b);
        vis[b.z][b.x][b.y]=1;
        while(!q.empty())
        {
            b=q.front();
            q.pop();
            for(int i=0;i<6;i++)
            {
                next.x=b.x+my_next[i][0];
                next.y=b.y+my_next[i][1];
                next.z=b.z+my_next[i][2];
                next.step=b.step+1;
          		if(next.x==e.x&&next.y==e.y&&next.z==e.z)
        	    {
        	        ans=next.step;
        	        goto out;
        	    }
            	if(check(next))
          	    {
           		    vis[next.z][next.x][next.y]=1;
          	        q.push(next);
          	 	}
            } 
        }
        ans=-1;
        out:;
        if(ans!=-1)
        {
            printf("Escaped in %d minute(s).\n",ans);
        }
        else 
        {
            printf("Trapped!\n");
        }
    }
    return 0;
}

Final:Accepted(之前错了四次)

原因:第一次,check函数内没加vis数组判断,MLE,但是我以为是数组开大了,改了一下数组大小,wa

第二次:数组改了,但是没有加vis,wa

第三次:加了vis,不过没有注释掉freopen关键字,我以为是ans之类没有重置,(事实上也确实需要重置)wa

第四次:去掉freopen,通过了

猜你喜欢

转载自blog.csdn.net/qq_43484493/article/details/88364723