解题心得:

1、水题,主要主意好一个点就好。

2、注意x、y、z坐标的选取就好。

题目:

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

#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std; int map[55][55][55],use[55][55][55];
int a,b,c,time;
int key_y,key_z,key_x;
bool flag = 0;
int dir[6][3] = {0,0,-1,0,0,1,0,1,0,0,-1,0,1,0,0,-1,0,0};
struct node
{
int y,z,x;
int step;
}now,Next; void map_store()
{
scanf("%d%d%d",&a,&b,&c);
key_y = a-1;
key_z = b-1;
key_x = c-1;
scanf("%d",&time);
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
for(int k=0;k<c;k++)
{
scanf("%d",&map[i][j][k]);
}
}
}
now.y = 0;
now.z = 0;
now.x = 0;
now.step = 0;
} int check(int a1,int b1,int c1)
{
if(a1<0 || b1<0 || c1<0 || a1>=a || b1>=b || c1>=c || map[a1][b1][c1]==1)
return 0;
else
return 1;
} void BFS()
{
memset(use,0,sizeof(use));
queue <node> qu;
qu.push(now);
use[now.y][now.z][now.x] = 1;
while(!qu.empty())
{
now = qu.front();
qu.pop();
Next = now;
Next.step = now.step + 1;
if(Next.step > time)
{
flag = 1;
return;
}
for(int i=0;i<6;i++)
{
Next.y = now.y + dir[i][0];
Next.z = now.z + dir[i][1];
Next.x = now.x + dir[i][2];
if(check(Next.y,Next.z,Next.x))
{
if(Next.y == key_y && Next.z == key_z && Next.x == key_x)
{
printf("%d\n",Next.step);
return;
}
else if(!use[Next.y][Next.z][Next.x])
{
qu.push(Next);
use[Next.y][Next.z][Next.x] = 1;
}
}
}
}
flag = 1;
return;
} int main()
{
int t;
scanf("%d",&t);
while(t--)
{
flag = 0;
map_store();
BFS();
if(flag)
{
printf("%d\n",-1);
}
}
}

BFS:胜利大逃亡的更多相关文章

  1. bfs 胜利大逃亡

    http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目: Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一 ...

  2. hdu.1429.胜利大逃亡(续)(bfs + 0101011110)

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

  3. hdu 1429 胜利大逃亡(续)(bfs+位压缩)

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

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

    此题可以做为三维深搜模板题.. 胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot ...

  5. HDOJ1253 胜利大逃亡 BFS

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

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

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

  7. 胜利大逃亡(续)(状态压缩bfs)

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

  8. HDU1253 胜利大逃亡 BFS

    胜利大逃亡 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submiss ...

  9. 胜利大逃亡(续)(bfs+状态压缩)

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

  10. 胜利大逃亡(续)hdu1429(bfs)

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

随机推荐

  1. jmeter压测配置

    windows上面修改最大使用端口数和time_await等待时间 注册表需要添加两个配置,位置:HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ S ...

  2. Java项目—嗖嗖移动业务大厅

    嗖嗖移动业务大厅包类(如下图): SosoMgr: package cn.biz; import java.util.Scanner; import cn.common.Common; import ...

  3. 面向对象(OOP)二

    一.“魔术”函数 - 自动调用 魔术方法 在面向对象有一些特别的方法,无需特别定义,已自动具备某些功能,例如构造函数__construt,这些方法统称魔术方法,在日后的编程中,可以使用这些方法的特性设 ...

  4. jQuery开发插件的两个方法 js 深浅拷贝

    1.jQuery.extend(object);为扩展jQuery类本身.为类添加新的方法.由全局函数来调用, 主要是用来拓展个全局函数 2.jQuery.fn.extend(object);为jQu ...

  5. Oracle 用户相关

    1.查询所有未修改过密码的Oracle用户 SELECT * FROM dba_users_with_defpwd d, dba_users du WHERE du.account_status = ...

  6. ${fn:} 函数

    调用这样一个头文件<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions " ...

  7. /etc/syslog.conf日志配置文件详解

    //将info或更高级别的消息送到/var/log/messages,除了mail以外. //其中*是通配符,代表任何设备:none表示不对任何级别的信息进行记录. *.info;mail.none; ...

  8. 撸了个 django 数据迁移工具 django-supertube

    撸了个 django 数据迁移工具 django-supertube 支持字段映射和动态字段转化. 欢迎 star,issue https://github.com/FingerLiu/django- ...

  9. Exception handling 异常处理的本质

    异常处理的本质:状态回滚或者状态维护. https://en.wikipedia.org/wiki/Exception_handling In general, an exception breaks ...

  10. 2018.2.3 Centos 的vim好看的主题配置及JDK的安装配置

    这里用的是Centos7云服务器的系统 第一步登录 centos7 系统: 通过查看命令 rpm -qa | grep vi 第二步:检测是否已经安装过Vim: 输入命令:rpm -qa|grep v ...