POJ2251(BFS求最短路)

来源:互联网 发布:java 输出两位小数 编辑:程序博客网 时间:2024/05/17 04:03

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!

题意:给你一个三维迷宫,让你算出从‘S’到‘E’的最短路径,如果没有通路,则输出”Trapped!”,否则输出”Escaped in x minute(s).”,x为最短路经过的路径数。

题解:从终点开始BFS,搜索出每一个能到达的点到终点的最短路径,当全部搜索完时,如果起点没有被搜索到,说明不通,否则起点到终点的最短路就是所要求得的结果。

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <map>#include <queue>#include <cmath>#include <vector>using namespace std;struct POINT {    int l, r, c;    POINT (int l = 0, int r = 0, int c = 0) : l(l), r(r), c(c) {}};const int maxn = 35;int d[maxn][maxn][maxn], vis[maxn][maxn][maxn], l, r, c;char maze[maxn][maxn][maxn];POINT s, e;void BFS(){    POINT cur;    queue<POINT> q;    q.push(e);    vis[e.l][e.r][e.c] = 1;    d[e.l][e.r][e.c] = 0;    while (!q.empty()) {        cur = q.front(); q.pop();        //printf ("%d %d %d ", cur.l, cur.r, cur.c);        //printf ("路径%d\n\n", d[cur.l][cur.r][cur.c]);        if (cur.c == s.c && cur.l == s.l && cur.r == s.r) {            printf ("Escaped in %d minute(s).\n", d[s.l][s.r][s.c]);            return;        }        if (cur.l + 1 < l && (maze[cur.l + 1][cur.r][cur.c] == '.' || maze[cur.l + 1][cur.r][cur.c] == 'S') && !vis[cur.l + 1][cur.r][cur.c]) {            vis[cur.l + 1][cur.r][cur.c] = 1;            d[cur.l + 1][cur.r][cur.c] = d[cur.l][cur.r][cur.c] + 1;            POINT next(cur.l + 1, cur.r, cur.c);            q.push(next);        }        if (cur.l - 1 >= 0 && (maze[cur.l - 1][cur.r][cur.c] == '.' || maze[cur.l - 1][cur.r][cur.c] == 'S') && !vis[cur.l - 1][cur.r][cur.c]) {            vis[cur.l - 1][cur.r][cur.c] = 1;            d[cur.l - 1][cur.r][cur.c] = d[cur.l][cur.r][cur.c] + 1;            POINT next(cur.l - 1, cur.r, cur.c);            q.push(next);        }        if (cur.r  + 1 < r && (maze[cur.l][cur.r + 1][cur.c] == '.'  || maze[cur.l][cur.r + 1][cur.c] == 'S') && !vis[cur.l][cur.r + 1][cur.c]) {            vis[cur.l][cur.r + 1][cur.c] = 1;            d[cur.l][cur.r + 1][cur.c] = d[cur.l][cur.r][cur.c] + 1;            POINT next(cur.l, cur.r + 1, cur.c);            q.push(next);        }        if (cur.r - 1 >= 0 && (maze[cur.l][cur.r - 1][cur.c] == '.'  || maze[cur.l][cur.r - 1][cur.c] == 'S') && !vis[cur.l][cur.r - 1][cur.c]) {            vis[cur.l][cur.r - 1][cur.c] = 1;            d[cur.l][cur.r - 1][cur.c] = d[cur.l][cur.r][cur.c] + 1;            POINT next(cur.l, cur.r - 1, cur.c);            q.push(next);        }        if (cur.c + 1 < c && (maze[cur.l][cur.r][cur.c + 1] == '.' || maze[cur.l][cur.r][cur.c + 1] == 'S') && !vis[cur.l][cur.r][cur.c + 1]) {            vis[cur.l][cur.r][cur.c + 1] = 1;            d[cur.l][cur.r][cur.c + 1] = d[cur.l][cur.r][cur.c] + 1;            POINT next(cur.l, cur.r, cur.c + 1);            q.push(next);        }        if (cur.c - 1 >= 0 && (maze[cur.l][cur.r][cur.c - 1] == '.' || maze[cur.l][cur.r][cur.c - 1] == 'S')&& !vis[cur.l][cur.r][cur.c - 1]) {            vis[cur.l][cur.r][cur.c - 1] = 1;            d[cur.l][cur.r][cur.c - 1] = d[cur.l][cur.r][cur.c] + 1;            POINT next(cur.l, cur.r, cur.c - 1);            q.push(next);        }    }    //printf ("%d\n\n", d[s.l][s.r][s.c]);    printf ("Trapped!\n");}int main(){    #ifndef ONLINE_JUDGE    freopen ("in.txt", "r", stdin);    #endif // ONLINE_JUDGE    while (scanf ("%d%d%d", &l, &r, &c) != EOF) {        if (l == 0 && r == 0 && c == 0) break;        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') {                        s.l = i;                        s.r = j;                        s.c = k;                    }                    if (maze[i][j][k] == 'E') {                        e.l = i;                        e.r = j;                        e.c = k;                    }                }            }        }        //printf ("S : %d %d %d\n\n", s.l, s.r, s.c);        //printf ("E : %d %d %d\n\n", e.l, e.r, e.c);        memset (vis, 0, sizeof(vis));        memset (d, -1, sizeof(d));        BFS ();    }    return 0;}
0 0