BFS:胜利大逃亡
解题心得:
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:胜利大逃亡的更多相关文章
- bfs 胜利大逃亡
http://acm.hdu.edu.cn/showproblem.php?pid=1253 题目: Ignatius被魔王抓走了,有一天魔王出差去了,这可是Ignatius逃亡的好机会. 魔王住在一 ...
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- hdu 1429 胜利大逃亡(续)(bfs+位压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU-1253 胜利大逃亡 (BFS)
此题可以做为三维深搜模板题.. 胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...
- HDOJ1253 胜利大逃亡 BFS
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submiss ...
- hdu 1253 胜利大逃亡 (三维简单bfs+剪枝)
胜利大逃亡 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU1253 胜利大逃亡 BFS
胜利大逃亡 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submiss ...
- 胜利大逃亡(续)(bfs+状态压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 胜利大逃亡(续)hdu1429(bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- GridView相同内容合并单元格
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web; ...
- 内容显示分页数字分页 aspx
此处是aspx里面分页显示,数据层和业务层是由动软生成 当然,我们也可以可以利用listView实现分页ListView(高效分页) public partial class NewList : Sy ...
- C#---vs2010发布、打包安装程序程序(转载)
转载地址:点击打开 1. 在vs2010 选择“新建项目”→“ 其他项目类型”→“ Visual Studio Installer→“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三 ...
- Java中的do-while循环——通过示例学习Java编程(11)
作者:CHAITANYA SINGH 来源:https://www.koofun.com/pro/kfpostsdetail?kfpostsid=22&cid=0 在上一篇教程中,我们讨论了w ...
- Alembic基本使用
1.alembic init YOUR_ALEMBIC_DIR 该目录下会有alembic.ini以及YOUR_ALEMBIC_DIR的目录. alembic.ini 提供了一些基本的配置 YOUR_ ...
- 【Quartus警告】created implicit net for XXX.
[警告内容]Warning (10236): Verilog HDL Implicit Net warning at forward_replace.v(16): created implicit n ...
- Exception handling 异常处理的本质
异常处理的本质:状态回滚或者状态维护. https://en.wikipedia.org/wiki/Exception_handling In general, an exception breaks ...
- 【洛谷1967】货车运输(最大生成树+倍增LCA)
点此看题面 大致题意: 有\(n\)个城市和\(m\)条道路,每条道路有一个限重.多组询问,每次询问从\(x\)到\(y\)的最大载重为多少. 一个贪心的想法 首先,让我们来贪心一波. 由于要求最大载 ...
- netbackup :nbu备份 Hyper-V 遇到快照错误(状态码 156)
遇到快照错误(状态码 156) 下表介绍与 NetBackup 状态码 156 有关的 Hyper-V 问题. 表:状态码 156 的可能原因 状态码 156 的原因 说明及推荐操作 NetBacku ...
- clearerr, feof, ferror, fileno - 检查以及重置流状态
总览 (SYNOPSIS) #include <stdio.h> void clearerr(FILE *stream); int feof(FILE *stream); int ferr ...