POJ - 2251 Dungeon Master(BFS)
发布日期:2021-10-03 15:44:49
浏览次数:2
分类:技术文章
本文共 1735 字,大约阅读时间需要 5 分钟。
题目大意:给你一个三维的迷宫,问能否逃出去
解题思路:纯BFS裸题
#include#include #include 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[N][N][N];bool vis[N][N][N];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, downint 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 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;}
转载地址:https://blog.csdn.net/L123012013048/article/details/49019791 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
表示我来过!
[***.240.166.169]2024年03月26日 22时04分26秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
使用Cacti监控你的网络Cacti的安装
2019-04-27
2011年6月编程语言关注度排行
2019-04-27
Varnish使用小结
2019-04-27
千万级并发HAproxy均衡负载系统介绍
2019-04-27
什么是A记录、MX记录、CNAME记录
2019-04-27
MongoDB简介
2019-04-27
Varnish purges 缓存清除
2019-04-27
Linux下redis安装部署
2019-04-27
水平切分与垂直切分
2019-04-27
MySQL引擎
2019-04-27
MySQL下的NoSQL解决方案HandlerSocket
2019-04-27
Apache服务器下使用 ab 命令进行压力测试
2019-04-27
查看Firefox中的缓存
2019-04-27
http header头设置反向代理不缓存
2019-04-27
配置MySQL主从复制
2019-04-27
CI框架如何删除地址栏的 index.php
2019-04-27
expires与etag控制页面缓存的优先级
2019-04-27
取消掉Transfer-Encoding:chunked
2019-04-27
HTTP协议中的Tranfer-Encoding:chunked编码解析
2019-04-27
JavaScript面向对象编程
2019-04-27