Pku2251 Dungeon Master

来源:互联网 发布:json赋值给新对象 编辑:程序博客网 时间:2024/05/19 07:27

/*





Dungeon Master
Time Limit: 1000MS        Memory Limit: 65536K
Total Submissions: 2410        Accepted: 1005

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!

Source
Ulm Local 1997*/








#include <iostream>
#include <string.h>
using namespace std;

char rocks[900][30];
int minute = 0;
int sign = 0;
int signs[900][30] = {0};

void DFS(int begini,int beginj,int endi,int endj,int L,int R,int C)
{
   
    if(begini == endi && beginj == endj)
    {
        sign = 1;
       
        return;
    }
    else
    {
        if(begini + R < R * L && rocks[begini + R][beginj] != '#' && signs[begini + R][beginj] == 0)
        {
            minute++;
            signs[begini + R][beginj] = 1;
            DFS(begini + R,beginj,endi,endj,L,R,C);
            if(sign == 1)
            {
                return;
            }
            minute--;
            signs[begini + R][beginj] = 0;
        }
        if(begini - R >= 0 && rocks[begini - R][beginj] != '#' && signs[begini - R][beginj] == 0)
        {
            minute++;
            signs[begini - R][beginj] = 1;
            DFS(begini - R,beginj,endi,endj,L,R,C);
            if(sign == 1)
            {
                return;
            }
            minute--;
            signs[begini - R][beginj] = 0;
        }
        if(begini - 1 >= 0 && rocks[begini - 1][beginj] != '#' && signs[begini - 1][beginj] == 0)
        {
            minute++;
            signs[begini - 1][beginj] = 1;
            DFS(begini - 1,beginj,endi,endj,L,R,C);
            if(sign == 1)
            {
                return;
            }
            minute--;
            signs[begini - 1][beginj] = 0;
        }
        if(begini + 1 < R * L && rocks[begini + 1][beginj] != '#' && signs[begini + 1][beginj] == 0)
        {
            minute++;
            signs[begini + 1][beginj] = 1;
            DFS(begini + 1,beginj,endi,endj,L,R,C);
            if(sign == 1)
            {
                return;
            }
            minute--;
            signs[begini + 1][beginj] = 0;
        }
        if(beginj - 1 >= 0 && rocks[begini][beginj - 1] != '#' && signs[begini][beginj - 1] == 0)
        {
            minute++;
            signs[begini][beginj - 1] = 1;
            DFS(begini,beginj - 1,endi,endj,L,R,C);
            if(sign == 1)
            {
                return;
            }
            minute--;
            signs[begini][beginj - 1] = 0;
        }
        if(beginj + 1 < C && rocks[begini][beginj + 1] != '#' && signs[begini][beginj + 1] == 0)
        {
            minute++;
            signs[begini][beginj + 1] = 1;
            DFS(begini,beginj + 1,endi,endj,L,R,C);
            if(sign == 1)
            {
                return;
            }
            minute--;
            signs[begini][beginj + 1] = 0;
        }
    }
    return;
}

int main(void)
{
    int L,R,C;
    int begini,beginj,endi,endj;
    memset(rocks,'#',900 * 30 * sizeof(char));
    while(cin>>L>>R>>C)
    {
        if(L == 0)
        {
            break;
        }
        else
        {
            for(int i = 0;i < L;i++)
            {
                for(int j = 0;j < R;j++)
                {
                    for(int k = 0;k < C;k++)
                    {
                        cin>>rocks[j + i * R][k];
                        if(rocks[j + i * R][k] == 'S')
                        {
                            begini = j + i * R;
                            beginj = k;
                            signs[begini][beginj] = 1;
                        }
                        else if(rocks[j + i * R][k] == 'E')
                        {
                            endi = j + i * R;
                            endj = k;
                        }
   
                    }
                }
            }
   
            DFS(begini,beginj,endi,endj,L,R,C);
            if(sign == 1)
            {
                cout<<"Escaped in "<<minute<<" minute(s)."<<endl;
            }
            else
            {
                cout<<"Trapped!"<<endl;
            }
            sign = 0;
            memset(rocks,'#',900 * 30 * sizeof(char));POJ2251 Dungeon Master
            memset(signs,0,900 * 30 * sizeof(int));
            minute = 0;


        }
    }

    return 0;
}

 

 

我DFS。。。。TLE了。。。。。等写完进讨论才看到一个个都说不能DFS要BFS。。。。嘛先这么着吧。。。。。