HOJ 1448
来源:互联网 发布:感情网络用语有哪些? 编辑:程序博客网 时间:2024/06/15 23:04
小测ProblemB。
传送门:http://acm.hit.edu.cn/hojx/showproblem/1448/
1448 - Dungeon Master
Time limit : 1 s Memory limit : 32 mb
Submitted : 643 Accepted : 324
Submit
Problem 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
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.
L, R, C <= 35
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
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“走迷宫类”搜索题,只不过把迷宫变成了三维的,相较于二维的多了两个方向,只要把地图和到达点变成三维的就可以了。
不过这里有一个小问题,WA了两次,后来我把X,Y,Z的划分调了一下后就AC了。。。
也不知道到底是什么原因,回去好好看看。。。
立个flag,过几天把做的一些BFS的题目写个总结。
AC代码:
#include <cstdio>#include <cstring>#include <queue>#define maxn 37using namespace std;int l, r, c;char dun[maxn][maxn][maxn];int get[maxn][maxn][maxn];int sx, sy, sz;struct Node{ int x, y, z; int step;};int dx[6] = {0, 0, 1, -1, 0, 0};int dy[6] = {1, -1, 0, 0, 0, 0};int dz[6] = {0, 0, 0, 0, 1, -1};queue<Node> escape;int BFS(void){ while(!escape.empty()) escape.pop(); memset(get, 0, sizeof(get)); Node beg; beg.x = sx, beg.y = sy, beg.z = sz, beg.step = 0; escape.push(beg); get[beg.x][beg.y][beg.z] = 1; while(!escape.empty()) { Node cur = escape.front(); escape.pop(); if(dun[cur.x][cur.y][cur.z] == 'E') return cur.step; for(int i = 0; i < 6; i++) { Node next; next.x = cur.x + dx[i]; next.y = cur.y + dy[i]; next.z = cur.z + dz[i]; if(next.x < 0 || next.x >= l || next.y < 0 || next.y >= r || next.z < 0 || next.z >= c) continue; if(get[next.x][next.y][next.z] || dun[next.x][next.y][next.z] == '#') continue; get[next.x][next.y][next.z] = 1; next.step = cur.step + 1; escape.push(next); } } return -1;}int main(){ while(scanf("%d %d %d", &l, &r, &c) != EOF && (l || r || c)) { for(int i = 0; i < l; i++) { for(int j = 0; j < r; j++) { scanf("%s", dun[i][j]); for(int h = 0; h < c; h++) { if(dun[i][j][h] == 'S') { sx = i; sy = j; sz = h; } } } } int ans = BFS(); if(ans == -1) printf("Trapped!\n"); else printf("Escaped in %d minute(s).\n", ans); } return 0;}
- HOJ 1448
- Hoj 1448 Dungeon Master
- HOJ
- 搜索(bfs) HOJ 1448 Dungeon Master
- 【HOJ 1448 Dungeon Master(BFS)】
- HOJ 1004
- 位图-hoj
- HOJ 1020
- HOJ Megaminx
- hoj WERTYU
- hoj 12033
- HOJ-2277
- HOJ-1962
- hoj 1408
- hoj 1006
- hoj 1138
- hoj 1999
- hoj(1797)
- dpkg 被中断,您必须手工运行 sudo dpkg –configure -a解决此问题
- 我的第一篇博客:我是怎么进入前端领域的
- 4-1 计算符号函数的值
- 工控ARM板登陆、wifi连接
- 【Scikit-Learn 中文文档】协方差估计 / 经验协方差 / 收敛协方差 / 稀疏逆协方差 / Robust 协方差估计
- HOJ 1448
- 工厂方法模式
- python小知识
- Find Patterns Forming Clumps in a String
- 4-2 计算分段函数[1]
- 典型的程序员面试题
- 11月28日心得
- Linux C操作mysql
- 【Scikit-Learn 中文文档】新异类和异常值检测