这个问题一看,可以说和UVA532 Dungeon Master完全相同,豪情万丈地做了拷贝来程序修改,一提交结果是“Time Limit Exceeded”,满脸困惑。多方调查研究后,终于懂得了程序简洁才是硬道理。也许因为测试数据量大,各个方面改进速度的措施都用了之后,总算是AC了。胜利大逃亡,逃出来了!

问题链接:HDU1253 胜利大逃亡。

问题简述:三维城堡(迷宫),每个点由0(可以经过)和1(墙)组成。输入测试用例数,输入每个例子的立体长宽高和限定的时间,起点是<1,1,1>,终点是<长,宽l高>,移动方向有上、下、左、右、前和后6个方向。每移动一次耗费1分钟,问能否在限定时间内最快走出。如果能则输出最短时间,不能则输出-1。

问题分析:一个三维迷宫,典型的BFS问题。在BFS搜索过程中,走过的点就不必再走了,因为这次再走下去不可能比上次的步数少。

程序说明:程序中,增加了边界使得判定条件变得简单;0和1的值对换了一下,判定条件也简单了;限定时间条件也用上了,可以减少展开的节点数量。这个程序需要处处节省时间。

把没有AC的程序也放在这里,可以看出逻辑上没有多少不同,就是不够简洁,结果是超时。

AC的C++语言程序如下:

/* HDU1253 胜利大逃亡 */#include <iostream>
#include <queue>
#include <cstdio>
#include <cstring>using namespace std;const int DIRECTSIZE = 6;
struct direct {int dx;int dy;int dz;
} direct[DIRECTSIZE] ={{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};const int MAXN = 50;
int cube[MAXN+2][MAXN+2][MAXN+2];struct node {int x, y, z, level;
};int L, R, C, limit;
node start;int bfs()
{queue<node> q;start.x = 1;start.y = 1;start.z = 1;start.level = 0;q.push(start);cube[1][1][1] = 0;while(!q.empty()) {node front = q.front();q.pop();for(int i=0; i<DIRECTSIZE; i++) {int nextx, nexty, nextz;nextx = front.x + direct[i].dx;nexty = front.y + direct[i].dy;nextz = front.z + direct[i].dz;if(cube[nextx][nexty][nextz]) {if(nextx == L && nexty == R && nextz == C)return front.level + 1;if(front.level < limit) {cube[nextx][nexty][nextz] = 0;node v;v.x = nextx;v.y = nexty;v.z = nextz;v.level = front.level + 1;q.push(v);}}}}return -1;
}int main()
{int t, ans, i, j, k;scanf("%d", &t);while(t--) {scanf("%d%d%d%d", &L, &R, &C, &limit);memset(cube, 0, sizeof(cube));for(i=1; i<=L; i++)for(j=1; j<=R; j++)for(k=1; k<=C; k++) {scanf("%d", &cube[i][j][k]);cube[i][j][k] = 1 - cube[i][j][k];}ans = bfs();printf("%d\n", ans);}return 0;
}

没有AC的C++语言程序(Time Limit Exceeded)如下:

/* HDU1253 胜利大逃亡 */#include <iostream>
#include <queue>
#include <cstdio>using namespace std;const int DIRECTSIZE = 6;
struct direct {int dx;int dy;int dz;
} direct[DIRECTSIZE] ={{-1, 0, 0}, {1, 0, 0}, {0, -1, 0}, {0, 1, 0}, {0, 0, -1}, {0, 0, 1}};const int MAXN = 50;
int cube[MAXN][MAXN][MAXN];struct node {int x, y, z, level;
};int L, R, C, limit;
node start, e2;
int ans;void bfs()
{queue<node> q;ans = -1;start.x = 0;start.y = 0;start.z = 0;start.level = 0;e2.x = L - 1;e2.y = R - 1;e2.z = C - 1;q.push(start);cube[0][0][0] = 1;while(!q.empty()) {node front = q.front();q.pop();for(int i=0; i<DIRECTSIZE; i++) {int nextx, nexty, nextz;nextx = front.x + direct[i].dx;if(0 > nextx || nextx >= L)continue;nexty = front.y + direct[i].dy;if(0 > nexty || nexty >= R)continue;nextz = front.z + direct[i].dz;if(0 > nextz || nextz >= C)continue;if(nextx == e2.x && nexty == e2.y && nextz == e2.z) {ans = front.level + 1;break;} else if(front.level < limit && cube[nextx][nexty][nextz] == 0) {cube[nextx][nexty][nextz] = 1;node v;v.x = nextx;v.y = nexty;v.z = nextz;v.level = front.level + 1;q.push(v);}if(ans > 0)break;}}
}int main()
{int t, i, j, k;scanf("%d", &t);while(t--) {scanf("%d%d%d%d", &L, &R, &C, &limit);for(i=0; i<L; i++)for(j=0; j<R; j++)for(k=0; k<C; k++)scanf("%d", &cube[i][j][k]);bfs();cout << ans << endl;}return 0;
}

转载于:https://www.cnblogs.com/tigerisland/p/7564434.html

HDU1253 胜利大逃亡相关推荐

  1. HDU1253 胜利大逃亡【BFS】

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  2. HDU-1253 胜利大逃亡 (BFS)

    好吧,上次刚傻完这次又傻了一次,先粘题目吧. 题目:HDU-1253 胜利大逃亡 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目: 胜利大 ...

  3. HDU1253 胜利大逃亡(DFS)

    这道题貌似大家都是用BFS写的,因为最近在练习DFS,所以就用DFS写了,虽然自己觉得已经剪枝了,但是貌似效果不是很好,还是有1000+ms,以后会用BFS再写一次的 #include<stdi ...

  4. 胜利大逃亡[HDU1253]

    胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Subm ...

  5. 【搜索入门专题1】hdu1253 【BFS】 F - 胜利大逃亡

    胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示 ...

  6. HDOJ 1253 HDU 1253 胜利大逃亡 ACM 1253 IN HDU

    MiYu原创, 转帖请注明 : 转载自 ______________白白の屋   题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目描述: ...

  7. HDU 1429 胜利大逃亡(续) (BFS+位压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others)  ...

  8. [ACM] hdu 1253 胜利大逃亡 (三维BFS)

    胜利大逃亡 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这但是Ignatius逃亡的好机会. 魔王住在一个城堡里,城堡是一个A*B*C的立方体,能够被表示 ...

  9. HDU 1253 胜利大逃亡

    胜利大逃亡 Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 1253 ...

最新文章

  1. let 与 expr Shell运算比较 let强强胜出
  2. idea删除后能还原吗_热水管能过100度热水吗
  3. 部队人员官兵请假管理系统
  4. stm32 stm8 产品型号
  5. 互联网晚报 | 3月1日 星期二 |​ 2022年苹果可能推出三款Apple Watch;“国家中小学智慧教育平台”投入试运行...
  6. 在国内 PMP 有多少含金量?分析+资料分享
  7. 什么是微信二次开发,如何进行微信二次开发?
  8. 用R做meta分析(附效应量计算神器)
  9. Https 证书相关
  10. uniapp拍照上传照片流程笔记
  11. 百度推广关键字质量度优化指南
  12. phpcms 下载模型列表页直接点击下载
  13. 我编写的肺炎疫情数据API的实现过程
  14. 解决win10每次重启后桌面图标排列混乱的问题。
  15. 电脑怎么修改html5,详细教你怎么设置电脑默认浏览器
  16. 抓取中国天气网当前时段所有城市的天气数据(python+xpath)
  17. 【翻译】Chromium 网络栈 disk cache 设计原理
  18. 填表统计预约打卡表单系统
  19. 链路不通或服务器没响应,连不通服务器服务怎么办(理论篇)
  20. 用三种方式安装Nginx

热门文章

  1. 百度SEO网页背景渐变色代码
  2. JMW-Label标签设计打印源码
  3. Quartz_简单编程式任务调度使用(SimpleTrigger)
  4. 移动开发在路上-- IOS移动开发系列 多线程二
  5. QQ网页登陆密码加密方式(农场、空间、WebQQ等通用)
  6. 用XenoCode 2006 加密dll(.NET
  7. [Magento error] The url is not accessible, unable to read response
  8. 使用 jQuery Mobile 与 HTML5 开发 Web App (十四) —— jQuery Mobile 方法下
  9. PHP header发送各种类型文件下载
  10. Leetcode算法题(C语言)17--验证回文字符串