NYOJ 353 3D dungeon (BFS)

来源:互联网 发布:知乎反对能看到吗 编辑:程序博客网 时间:2024/05/10 20:43

3D dungeon

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
描述
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? 
输入
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.
输出
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!
样例输入
3 4 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0
样例输出
Escaped in 11 minute(s).Trapped!

题意:给你一个高L长R宽C的图形,每一个坐标都可以视为一个方格,你一次可以向上,下,左,右,前,后任一方向移动一个方格, 但是不能向有#标记的方格移动。问:从S出发能不能到达E,如果能请输出最少的移动次数。

注意:因为是求最少的移动次数,所以要从所有能到达的中选出最少的。

 #include <iostream>#include <cstring>#include <algorithm>#include <queue>using namespace std;struct node{    int x, y, z;    int step;};node s, e;queue<node> q;int l, r, c;char maze[35][35][35];int vis[35][35][35];int dir[6][3] = {{1,0,0}, {-1,0,0}, {0,1,0}, {0,-1,0}, {0,0,1}, {0,0,-1}};int flag;int ans, min1;void bfs(){    q.push(s);    vis[s.z][s.x][s.y] = 1;    while (!q.empty()){        node temp = q.front();        for (int i = 0; i < 6; i++){            node temp1;            temp1.z = temp.z + dir[i][0];            temp1.x = temp.x + dir[i][1];            temp1.y = temp.y + dir[i][2];            temp1.step = temp.step + 1;            if (temp1.z >= 0 && temp1.z < l && temp1.x >= 0 && temp1.x < r && temp1.y >= 0 && temp1.y < c && !vis[temp1.z][temp1.x][temp1.y] && maze[temp1.z][temp1.x][temp1.y] != '#'){                if (temp1.z == e.z && temp1.x == e.x && temp1.y == e.y){                    flag = 1;                    if (temp1.step < min1)                        min1 = temp1.step;                }                else{                    q.push(temp1);                    vis[temp1.z][temp1.x][temp1.y] = 1;                }            }        }        q.pop();    }    return;}int main(){    while (cin >> l >> r >> c, l + r + c){        memset(vis, 0, sizeof(vis));        ans = 0, flag = 0, min1 = 99999;        for (int i = 0; i < l; i++){            for (int j = 0; j < r; j++){                cin >> maze[i][j];                for (int k = 0; k < c; k++){                    if (maze[i][j][k] == 'S'){                        s.z = i, s.x = j, s.y = k;                    }                    if (maze[i][j][k] == 'E'){                        e.z = i, e.x = j, e.y = k;                    }                }            }        }        bfs();        if (flag)            cout << "Escaped in " << min1 << " minute(s)." << endl;        else            cout << "Trapped!" << endl;    }    return 0;}        


0 0