poj 2251 Dungeon Master 【bfs基础题】
来源:互联网 发布:java简单图书系统代码 编辑:程序博客网 时间:2024/06/05 00:39
Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 18949 Accepted: 7344
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基础题:
#include <cstdio>#include <cstring>#include <cmath>#include <queue>#include <algorithm>using namespace std;int vis[31][31][31];char map[31][31][31];int sx, sy, sz, ex, ey, ez;int L, R, C;struct node{ int x, y, z, step; friend bool operator < (node a, node b) { return a.step > b.step; }};void getmap(){ int i, j, k; //getchar(); for(i = 0; i < L; i++) { for(j = 0; j < R; j++) { getchar(); for(k = 0; k < C; k++) { scanf("%c", &map[i][j][k]); if(map[i][j][k] == 'S') { sx = i; sy = j; sz = k; } if(map[i][j][k] == 'E') { ex = i; ey = j; ez = k; } } //getchar(); } getchar(); }}int judge(int x, int y, int z){ if(map[x][y][z] != '#' && !vis[x][y][z] && x >= 0 && x < L && y >= 0 && y < R && z >= 0 && z < C) return 1; else return 0;}void bfs(){ node now, next; priority_queue<node> q; int i, j, k; int move[6][3] = {1,0,0, -1,0,0, 0,1,0, 0,-1,0, 0,0,1, 0,0,-1}; memset(vis, 0, sizeof(vis)); now.x = sx; now.y = sy; now.z = sz; now.step = 0; q.push(now); vis[sx][sy][sz] = 1; while(!q.empty()) { now = q.top(); q.pop(); if(now.x == ex && now.y == ey && now.z == ez) { printf("Escaped in %d minute(s).\n", now.step); return ; } for(k = 0; k < 6; k++) { next.x = now.x + move[k][0]; next.y = now.y + move[k][1]; next.z = now.z + move[k][2]; if(judge(next.x, next.y, next.z)) { next.step = now.step + 1; vis[next.x][next.y][next.z] = 1; q.push(next); } } } printf("Trapped!\n");}int main(){ while(scanf("%d%d%d", &L, &R, &C),L||R||C) { getmap(); bfs(); } return 0;}
0 0
- poj 2251 Dungeon Master 【bfs基础题】
- POJ 2251 Dungeon Master 三维空间BFS(基础题)
- POJ 2251 Dungeon Master BFS
- POJ 2251 Dungeon Master (BFS)
- POJ 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master【BFS】
- POJ 2251 Dungeon Master(BFS)
- poj 2251 Dungeon Master(bfs)
- POJ 2251--Dungeon Master【BFS】
- poj 2251 Dungeon Master 【BFS】
- POJ 2251:Dungeon Master【bfs】
- poj-2251-Dungeon Master-BFS
- poj-2251 Dungeon Master BFS
- poj 2251 Dungeon Master(BFS)
- POJ 2251 Dungeon Master (BFS)
- POJ 2251 Dungeon Master(bfs)
- POJ - 2251 Dungeon Master(BFS)
- poj 2251 Dungeon Master(bfs)
- 数据库的内连接和外连接
- A new concept: dynamic SQL
- 如何捕获 System.loadLibrary 产生的异常?
- ZOJ 3872 浙江2015年省赛试题
- Android仿微信底部菜单栏+顶部菜单栏(附源码)
- poj 2251 Dungeon Master 【bfs基础题】
- Ubuntu 14.04 Web服务器--Apache的安装和配置
- Qt 坐标轴 平滑曲线 贝塞尔曲线
- rhel6.5 下samba安装配置
- php中url字符串的一些解析函数
- hsacm-1441
- 大话计算机网络网络层的概念
- android清除缓存
- VC中使用OnSize函数,设置控件位置的注意事项