POJ2251 Dungeon Master
来源:互联网 发布:淘宝女装拍照技巧 编辑:程序博客网 时间:2024/06/06 21:39
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 36512 Accepted: 13921
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?
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.
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
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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
/*************************************************************************> File Name: poj2251.cpp> Author: YinJianxiang> Mail: YinJianxiang123@gmail.com> Created Time: 2017年07月26日 星期三 00时37分53秒 ************************************************************************/#include <iostream> #include <queue> #include <map> #include <stack> #include <cstdio> #include <set> #include <string> #include <cstring> using namespace std; typedef long long ll; int x,y,z; typedef struct node { int x; int y; int z; int step; } node; node begin; node end; queue<node> que; bool visited[35][35][35]; string s[35][35]; int dx[] = {1,-1,0,0,0,0}; int dy[] = {0,0,1,-1,0,0}; int dz[] = {0,0,0,0,1,-1}; void clearQueue() { while(!que.empty()) { que.pop(); } } bool isSafe(int tx,int ty,int tz) { if(tx >= 0 && tx < x && ty >= 0 && ty < y && tz >= 0 && tz < z && visited[tx][ty][tz] == true) { return true; } return false; } int bfs() { que.push(begin); visited[begin.x][begin.y][begin.z] = false; while(!que.empty()) { node front = que.front(); //cout << front.x << front.y << front.z << endl; que.pop(); if(front.x == end.x && front.y == end.y && front.z == end.z) { //cout << "asdfg" << endl; return front.step; } for(int i = 0;i < 6;i++) { node next; next.x = front.x + dx[i]; next.y = front.y + dy[i]; next.z = front.z + dz[i]; next.step = front.step + 1; if(isSafe(next.x,next.y,next.z) == true) { que.push(next); visited[next.x][next.y][next.z] = false; } } } return -1; } int main() { while(cin >> x >> y >> z) { if(x == 0 && y == 0 && z == 0) { break; } clearQueue(); memset(visited,true,sizeof(visited)); for(int i = 0;i < x;i++) { for(int j = 0;j < y;j++) { cin >> s[i][j]; for(int k = 0;k < z;k++) { if(s[i][j][k] == '#') { visited[i][j][k] = false; } if(s[i][j][k] == '.') { visited[i][j][k] = true; } if (s[i][j][k] == 'S'){ begin.x = i; begin.y = j; begin.z = k; begin.step = 0; visited[i][j][k] = true; } else if (s[i][j][k] == 'E'){ end.x = i; end.y = j; end.z = k; end.step = 0; visited[i][j][k] = true; //cout << end.x << end.y << end.z << "\n" << "\n"; } } } } int t = bfs(); if (t == -1) { cout << "Trapped!" << "\n"; } else { cout << "Escaped in " << t << " minute(s)." << "\n"; } } return 0; }
阅读全文
0 0
- POJ2251 Dungeon Master
- POJ2251 Dungeon Master
- poj2251 Dungeon Master (BFS)
- poj2251 - Dungeon Master
- poj2251 Dungeon Master
- POJ2251:Dungeon Master
- Dungeon Master(poj2251,bfs)
- Dungeon Master(poj2251)
- poj2251 Dungeon Master
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master bfs
- POJ2251:Dungeon Master(BFS)
- poj2251 Dungeon Master(BFS)
- POJ2251 Dungeon Master BFS
- poj2251 Dungeon Master
- POJ2251---Dungeon Master
- [bfs]poj2251 Dungeon Master
- POJ2251----Dungeon Master
- Redis系列二数据类型(redis数据类型)
- 128. Longest Consecutive Sequence
- 常用的linux命令
- ubuntu VMware
- LigerUI
- POJ2251 Dungeon Master
- HDU
- JVM 监控 jmxtrans 和 jmxtrans-agent
- linux ubuntu mount success
- Hibernate(十一)Criteria查询
- Android查看手机内部储存目录及数据库文件
- ubuntu配置Android指南
- sh和source的区别
- 【Python】用pip安装python库下载超时的解决办法