POJ - 2251 Dungeon Master(BFS)
2015-10-10 10:22
435 查看
题目大意:给你一个三维的迷宫,问能否逃出去
解题思路:纯BFS裸题
解题思路:纯BFS裸题
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int N = 35; struct Node{ int x, y, z, time; Node() {} Node(int x, int y, int z, int time): x(x), y(y), z(z), time(time) {} }; char map ; bool vis ; int dir[6][3] = {{1, 0, 0}, {-1, 0, 0}, {0, 0, -1}, {0, 0, 1}, {0, -1, 0}, {0, 1, 0}};//up, down, left, right, front, down int L, R, C, sx, sy, sz, ex, ey, ez; void init() { for (int i = 1; i <= L; i++) for (int j = 1; j <= R; j++) { scanf("%s", map[i][j] + 1); for (int k = 1; k <= C; k++) if (map[i][j][k] == 'S') { sx = i; sy = j; sz = k; } else if (map[i][j][k] == 'E') { ex = i; ey = j; ez = k; } } } void solve() { queue<Node> Q; Q.push(Node(sx, sy, sz, 0)); memset(vis, 0, sizeof(vis)); vis[sx][sy][sz] = 1; while (!Q.empty()) { Node t = Q.front(); Q.pop(); int x = t.x; int y = t.y; int z = t.z; if (x == ex && y == ey && z == ez) { printf("Escaped in %d minute(s).\n", t.time); return ; } for (int i = 0; i < 6; i++) { int tx = x + dir[i][0]; int ty = y + dir[i][1]; int tz = z + dir[i][2]; if (tx <= 0 || tx > L || ty <= 0 || ty > R || tz <= 0 || tz > C || vis[tx][ty][tz] || map[tx][ty][tz] == '#') continue; vis[tx][ty][tz] = 1; Q.push(Node(tx, ty, tz, t.time + 1)); } } printf("Trapped!\n"); } int main() { while (scanf("%d%d%d", &L, &R, &C) != EOF && L + R + C) { init(); solve(); } return 0; }
相关文章推荐
- 马哥运维笔记----6
- 重写barButton的set方法修改导航按钮的左右边距。
- log4j2.x日志按日期分隔
- Cell的使用大总结
- Monkey log异常分析说明
- 关于NSURL拼接字符串的问题
- 使用Eclipsephp工具打开Thinkphp为什么会有红色的波浪线报警?
- Lua查找表元素过程(元表、__index方法是如何工作的)
- MapboxGL采用的Web Workers策略及其调试方法
- 打开新的portal的方法【BeX5.3.2】
- 缺陷跟踪系统Mantis Bug Tracker
- trim()函数
- _beginThreadex的用法
- 自定义之-对话框
- NSdata 与 NSString,Byte数组,UIImage 的相互转换—ios开发 Objective-C
- Tomcat安装和常见问题
- HttpClient_4 用法 由HttpClient_3 升级到 HttpClient_4 必看
- LeetCode----Combination Sum III
- POJ - 1321 棋盘问题(DFS)
- Goldengate for oracle 数据一致性初始化方案