题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Problem Description

Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会.

魔王住在一个城堡里,城堡是一个A*B*C的立方体,可以被表示成A个B*C的矩阵,刚开始Ignatius被关在(0,0,0)的位置,离开城堡的门在(A-1,B-1,C-1)的位置,现在知道魔王将在T分钟后回到城堡,Ignatius每分钟能从一个坐标走到相邻的六个坐标中的其中一个.现在给你城堡的地图,请你计算出Ignatius能否在魔王回来前离开城堡(只要走到出口就算离开城堡,如果走到出口的时候魔王刚好回来也算逃亡成功),如果可以请输出需要多少分钟才能离开,如果不能则输出-1.

Input

输入数据的第一行是一个正整数K,表明测试数据的数量.每组测试数据的第一行是四个正整数A,B,C和T(1<=A,B,C<=50,1<=T<=1000),它们分别代表城堡的大小和魔王回来的时间.然后是A块输入数据(先是第0块,然后是第1块,第2块......),每块输入数据有B行,每行有C个正整数,代表迷宫的布局,其中0代表路,1代表墙.(如果对输入描述不清楚,可以参考Sample Input中的迷宫描述,它表示的就是上图中的迷宫)

特别注意:本题的测试数据非常大,请使用scanf输入,我不能保证使用cin能不超时.在本OJ上请使用Visual C++提交.

Output

对于每组测试数据,如果Ignatius能够在魔王回来前离开城堡,那么请输出他最少需要多少分钟,否则输出-1.

Sample Input

1
3 3 4 20
0 1 1 1
0 0 1 1
0 1 1 1
1 1 1 1
1 0 0 1
0 1 1 1
0 0 0 0
0 1 1 0
0 1 1 0

Sample Output

11

Problem solving report:

Description: 判断在t秒内能不能从(0, 0, 0)到达(A-1, B-1, C-1)。
Problem solving: 深搜和广搜都可以,广搜好写,深搜的话必须要剪枝,记忆化搜索。

Accepted Code:

//DFS
#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int l, r, c, t, min_;
int dp[55][55][35], mp[55][55][55];
int dir[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}};
void DFS(int x, int y, int z, int s) {int tx, ty, tz;if (x + y + z + t < l + r + c + s - 3 || s > t || s >= min_)return ;if (x == l - 1 && y == r - 1 && z == c - 1) {min_ = s;return ;}for (int i = 0; i < 6; i++) {tx = x + dir[i][0];ty = y + dir[i][1];tz = z + dir[i][2];if (tx >= 0 && tx < l && ty >= 0 && ty < r && tz >= 0 && tz < c && !mp[tx][ty][tz] && (dp[tx][ty][tz] > s + 1 || !dp[tx][ty][tz])) {mp[tx][ty][tz] = 1;dp[tx][ty][tz] = s + 1;DFS(tx, ty, tz, s + 1);mp[tx][ty][tz] = 0;}}
}
int main() {int kase;scanf("%d", &kase);while (kase--) {min_ = inf;scanf("%d%d%d%d", &l, &r, &c, &t);memset(dp, 0, sizeof(dp));for (int i = 0; i < l; i++)for (int j = 0; j < r; j++)for (int k = 0; k < c; k++)scanf("%d", &mp[i][j][k]);DFS(0, 0, 0, 0);if (min_ < inf)printf("%d\n", min_);else printf("-1\n");}return 0;
}
//BFS
#include <bits/stdc++.h>
using namespace std;
bool vis[55][55][55];
int l, r, c, t, mp[55][55][55];
int dir[6][3] = {{0, 0, 1}, {0, 0, -1}, {0, 1, 0}, {0, -1, 0}, {1, 0, 0}, {-1, 0, 0}};
struct edge {int x, y, z, t;edge(int x_, int y_, int z_, int t_) {x = x_, y = y_, z = z_, t = t_;}
}p(0, 0, 0, 0);
int BFS(int x, int y, int z) {int tx, ty, tz;queue <edge> Q;vis[x][y][z] = true;Q.push(edge(x, y, z, 0));while (!Q.empty()) {p = Q.front();Q.pop();if (p.x == l - 1 && p.y == r - 1 && p.z == c - 1)return p.t;if (p.t >= t)return -1;for (int i = 0; i < 6; i++) {tx = p.x + dir[i][0];ty = p.y + dir[i][1];tz = p.z + dir[i][2];if (tx >= 0 && tx < l && ty >= 0 && ty < r && tz >= 0 && tz < c && mp[tx][ty][tz] != 1 && !vis[tx][ty][tz]) {vis[tx][ty][tz] = true;Q.push(edge(tx, ty, tz, p.t + 1));}}}return -1;
}
int main() {int kase;scanf("%d", &kase);while (kase--) {scanf("%d%d%d%d", &l, &r, &c, &t);memset(vis, false, sizeof(vis));for (int i = 0; i < l; i++)for (int j = 0; j < r; j++)for (int k = 0; k < c; k++)scanf("%d", &mp[i][j][k]);int ans = BFS(0, 0, 0);printf("%d\n", ans);}return 0;
}

HDU - 胜利大逃亡(搜索)相关推荐

  1. HDU 胜利大逃亡(BFS)

    HDOJ 1253 胜利大逃亡 (BFS) Problem Description BFS 完整代码 Problem Description Ignatius被魔王抓走了,有一天魔王出差去了,这可是I ...

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

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

  3. HDU 1253 胜利大逃亡

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

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

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

  5. HDU 1253 胜利大逃亡 题解

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

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

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

  7. HDU 1253 - 胜利大逃亡

    HDU 1253 - 胜利大逃亡 Problem: a* b* c 立方体,从(1,1,1)到(a,b,c),最短路<=限制时间 Solution: BFS Code: #include< ...

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

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

  9. HDU 1429 胜利大逃亡(续)

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

最新文章

  1. 【FFmpeg】使用过的命令汇总(持续更新中...)
  2. c++ mysql ctime_C++操作mysql数据库范例代码
  3. Windows Server 2008正式版[微软官方下载地址+官方语言包]
  4. 011_学生管理系统二
  5. 字符串周期--hdu 3746 Cyclic Nacklace
  6. 组织配置java项目的外部lib包
  7. 【渝粤教育】国家开放大学2018年春季 3780-22T燃气设备操作与维护 参考试题
  8. Windows驱动程序运行时函数的调用
  9. 【Spark】一条 SQL 在 Apache Spark 之旅(上)
  10. oracle默认端口号是,sqlserver、mysql、oracle各自的默认端口号
  11. 广搜4 ——Cheese
  12. Paddlehub一些简单应用
  13. Centos初学者需要会的几种命令(2)
  14. 大学生考软考合适吗?
  15. CoAP学习笔记(1)CoAP报文结构
  16. 红黑树RBT的原理分析及实现
  17. 安装SQLFULL_CHS2008 SQL Server
  18. 大牛们,哪里跑,悬疑片之--《大牛影踪》
  19. 解决MySQL中主键无法设置自增
  20. 2022年4月中国葡萄酒产量数据

热门文章

  1. 老司机都懂的x件事,一般人我不告诉他
  2. Excel实现数据可视化
  3. unity之摇杆和NPC
  4. win7安装JAVA程序闪退怎么办_win7打开软件闪退如何解决
  5. https协议谷歌浏览器使用Jmeter脚本录制
  6. MQTT学习笔记——MQTT协议使用
  7. 谷歌地图上不去了,用BIGEMAP就可以了
  8. C# 获取Excel工作薄中Sheet页(工作表)名集合
  9. 123.R简介和统计绘图
  10. 自动回复的三种形式?