POJ 2251 Dungeon Master (BFS)

来源:互联网 发布:《人工智能ai》 编辑:程序博客网 时间:2024/05/21 08:50
<span style="color: rgb(0, 0, 255); font-size: 24px; "><strong>Description</strong></span>
<span style="color: rgb(0, 0, 255); font-size: 24px; "><strong></strong></span>
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!


BFS水题一道,做了约一个星期搜索第一次1A的题目。三维数组模拟空间,六个方向搜索(上下东南西北)。

想把这几天做搜索时对DFS和BFS的理解说一下。DFS是按照路径来搜索的,一条路径走完了就回溯到上一个节点继续搜索下一条路径。而BFS是按照步数来搜索的,每条路径的进行几乎是同时的。


#include <iostream>#include <queue>#include <cstring>using namespace std;int L,R,C;int sl,sr,sc;int el,er,ec;int flag;struct POS{    int l,r,c;};char maze[32][32][32];const int INF = 0x7f7f7f7f;int dl[6] = {0,0,0,0,1,-1},dr[6] = {0,-1,0,1,0,0},dc[6] = {1,0,-1,0,0,0};int d[32][32][32];int BFS(){    memset(d,0x7f,sizeof(d));    queue<POS> que;    POS temp;    temp.l = sl,temp.r = sr,temp.c = sc;    que.push(temp);    d[sl][sr][sc] = 0;    while(!que.empty())    {        POS p = que.front();        que.pop();        if(p.l == el && p.r == er && p.c == ec)        {            flag = 1;            break;        }        for(int i = 0;i < 6;i++)        {            int nl = p.l + dl[i];            int nr = p.r + dr[i];            int nc = p.c + dc[i];            if(0 <= nl && nl <L && 0 <= nr && nr < R && 0 <= nc && nc < C && maze[nl][nr][nc] != '#' && d[nl][nr][nc] == INF)            {                d[nl][nr][nc] = d[p.l][p.r][p.c] + 1;                POS next;                next.l = nl,next.r = nr,next.c = nc;                que.push(next);            }        }    }}int main(){    while(cin >> L >> R >> C)    {        if(!(L || R || C))            break;        flag = 0;        for(int i = 0;i < L;i++)            for(int j = 0;j < R;j++)                for(int k = 0;k < C;k++)                {                    cin >> maze[i][j][k];                    if(maze[i][j][k] == 'S')                    {                        sl = i;                        sr = j;                        sc = k;                    }                    if(maze[i][j][k] == 'E')                    {                        el = i;                        er = j;                        ec = k;                    }                }        BFS();        if(flag)            cout << "Escaped in " << d[el][er][ec] << " minute(s)." << endl;        else            cout << "Trapped!" << endl;    }    return 0;}


0 0
原创粉丝点击