题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1253

思路分析:因为问题需要寻找到达终点的最短的距离(最短的步数),即在状态转换图上需要找出层次最浅的的状态(A-1, B-1, C-1),

所以采用bfs更快能找出答案;另外,若采用dfs则比较困难,需要遍历所有的状态才能找出结果。

代码如下

#include <cstdio>
#include <iostream> const int MAX_N = ;
struct Point
{
int a, b, c;
int time;
Point() { a = ; b = ; c = ; time = ; }
Point(int x, int y, int z, int step_time)
{
a = x; b = y; c = z; time = step_time;
}
}; int ans = ;
int A, B, C, game_times;
int map[MAX_N][MAX_N][MAX_N];
int move[][] =
{{, , -}, {, , }, {, -, }, {, , }, {-, , }, {, , }};
Point queue[]; int Bfs()
{
int head, tail;
Point temp; head = tail = ;
temp.a = temp.b = temp.c = temp.time = ;
queue[tail++] = temp; while (head != tail)
{
temp = queue[head++];
for (int i = ; i < ; ++i)
{
int next_a = temp.a + move[i][];
int next_b = temp.b + move[i][];
int next_c = temp.c + move[i][]; if (next_a < || next_b < || next_c <
|| next_a >= A || next_b >= B || next_c >= C
|| map[next_a][next_b][next_c] > || temp.time + > game_times)
continue; if (next_a == A - && next_b == B - && next_c == C - )
return temp.time + ; Point next(next_a, next_b, next_c, temp.time + );
map[next_a][next_b][next_c] = next.time;
queue[tail++] = next;
}
}
return -;
} int main()
{
int k; scanf("%d", &k);
while (k--)
{
scanf("%d %d %d %d", &A, &B, &C, &game_times);
for (int i = ; i < A; ++i)
for (int j = ; j < B; ++j)
for (int k = ; k < C; ++k)
scanf("%d", &map[i][j][k]); printf("%d\n", Bfs());
}
return ;
}

HDOJ 1253 胜利大逃亡(bfs)的更多相关文章

  1. hdoj 1253 胜利大逃亡

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

  2. hdu - 1240 Nightmare && hdu - 1253 胜利大逃亡(bfs)

    http://acm.hdu.edu.cn/showproblem.php?pid=1240 开始没仔细看题,看懂了发现就是一个裸的bfs,注意坐标是三维的,然后每次可以扩展出6个方向. 第一维代表在 ...

  3. HDU 1253 胜利大逃亡(BFS)

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

  4. Hdoj 1253.胜利大逃亡 题解

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

  5. ny523 亡命逃串 hdoj 1253胜利大逃亡

    亡命逃窜 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 从前有个叫hck的骑士,为了救我们美丽的公主,潜入魔王的老巢,够英雄吧.不过英雄不是这么好当的.这个可怜的娃被魔 ...

  6. hdoj 1429 胜利大逃亡(续) 【BFS+状态压缩】

    题目:pid=1429">hdoj 1429 胜利大逃亡(续) 同样题目: 题意:中文的,自己看 分析:题目是求最少的逃亡时间.确定用BFS 这个题目的难点在于有几个锁对于几把钥匙.唯 ...

  7. hdu 1253:胜利大逃亡(基础广搜BFS)

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

  8. HDU 1253 胜利大逃亡 NYOJ 523【BFS】

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

  9. hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)

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

随机推荐

  1. Java基础之静态变量

    public class StaticVariable { public static void main(String[] args) { Person p1 = new Person(); Per ...

  2. R与数据分析旧笔记(六)多元线性分析 上

    > x=iris[which(iris$Species=="setosa"),1:4] > plot(x) 首先是简单的肉眼观察数据之间相关性 多元回归相较于一元回归的 ...

  3. git配置ssh

    $ git config --global user.name "yourname"$ git config --global user.email "youremail ...

  4. GridView分页功能的实现

    当GridView中显示的记录很多的时候,可以通过GridView的分页功能来分页显示这些记录.如果GridView是直接绑定数据库,则很简单:将"启动分页"打勾即可. 如果是用代 ...

  5. java——%

    %; //结果为1 %; //结果为-1 ; //结果为1.2000000000000002

  6. Java中print、printf、println的区别(转载)

    printf主要是继承了C语言的printf的一些特性,可以进行格式化输出 print就是一般的标准输出,但是不换行 println和print基本没什么差别,就是最后会换行 System.out.p ...

  7. android 源代码快速搜索引擎OpenCrok

    使用OpenGrok的Android系统源码搜索引擎,搜索速度简直太快速了!: http://androidxref.com/ 另外,OpenGrok的也是可以在本地计算机中安装配置的,主要是安装To ...

  8. data pump(数据泵)

    先给出oracle给出的一个定义: “Oracle Data Pump technology enables very high-speed movement of data and metadata ...

  9. hdu 4738 Caocao's Bridges(2013杭州网络赛丶神坑)

    就是求最小权值的桥..不过有好几个坑... 1:原图不连通,ans=0. 2: m<=n^2 显然有重边,重边必然不是桥,处理重边直接add(u, v, INF). 3:   最小桥边权为0的时 ...

  10. tomcat 7 无法打开管理页面

    在配置文件tomcat-users.xml中添加如下内容即可. <role rolename="admin"/> <role rolename="man ...