HDU - 1253 胜利大逃亡(搜索)
2024-08-29 05:25:01
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
三维搜索,数据量不大,利用不超过T的时间剪枝即可,优化时间。
因为是多组输入所以慎用return 0,wa了7遍。
#include<iostream>
#include<queue>
#include<algorithm>
#include<set>
#include<cmath>
#include<vector>
#include<map>
#include<stack>
#include<bitset>
#include<cstdio>
#define Swap(a,b) a^=b^=a^=b
#define cini(n) scanf("%d",&n)
#define cinl(n) scanf("%lld",&n)
#define cinc(n) scanf("%c",&n)
#define coui(n) printf("%d",n)
#define couc(n) printf("%c",n)
#define coul(n) printf("%lld",n)
#define speed ios_base::sync_with_stdio(0);
#define Max(a,b) a>b?a:b
#define Min(a,b) a<b?a:b
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=1e6+10;
const double esp=1e-9;
int m,n,s,x,y,z,A,B,C;
int ob[51][51][51];
struct lo
{
int x,y,z,cnt;
lo(int a,int b,int c,int cnt ):x(a),y(b),z(c),cnt(cnt){}
};
int fx[6][3]={{1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1}};
int main()
{
int T;
cini(T);
while(T--)
{
cini(A);
cini(B);
cini(C);
cini(s);
for(int i=0;i<A;i++)
for(int j=0;j<B;j++)
for(int k=0;k<C;k++)
cini(ob[i][j][k]);
//while(cin>>x>>y>>z) cout<<ob[x][y][z]<<endl;
queue<lo> q;
while(!q.empty()) q.pop();
q.push(lo(0,0,0,0));
ob[0][0][0]=1;
while(!q.empty())
{
lo tem=q.front();
int a=tem.x;
int b=tem.y;
int c=tem.z;
int cnt=tem.cnt;
if(a==A-1&&b==B-1&&c==C-1)
{
printf("%d\n",cnt);
goto loop;
}
q.pop();
for(int i=0;i<6;i++)
{
if(a+fx[i][0]<0||a+fx[i][0]>=A||b+fx[i][1]<0||b+fx[i][1]>=B||c+fx[i][2]>=C||c+fx[i][2]<0) continue;
if(ob[a+fx[i][0]][b+fx[i][1]][c+fx[i][2]]==0&&cnt+1<=s)
{
// cout<<a+fx[i][0]<<' '<<b+fx[i][1]<<' '<<c+fx[i][2]<<endl;
ob[a+fx[i][0]][b+fx[i][1]][c+fx[i][2]]=1;
q.push(lo(a+fx[i][0],b+fx[i][1],c+fx[i][2],cnt+1));
}
}
}
printf("-1\n");
loop:continue;
}
}
最新文章
- Redhat6.4下安装Oracle10g
- (二)Netty源码学习笔记之服务端启动
- DOM性能瓶颈与Javascript性能优化
- 初学RunLoop
- [Android] Android统计Apk , jar包方法数
- TYVJ1288 飘飘乎居士取能量块 -SilverN
- 课堂所讲整理:Set和Map
- Android Studio 学习 - 基本控件的使用;Intent初学
- Java面试题之Class.forName的作用
- KVM guest caching modes
- 制作、烧写根文件系统,使用NFS,编译使用驱动程序
- nodejs 构建本地web测试服务器 以及 解决访问静态资源的问题!
- June. 26th 2018, Week 26th. Tuesday
- 正则表达式中引用shell变量
- idea2018版tomcat基本配置
- Error:(18, 51) java: -source 1.5 中不支持 diamond 运算符 (请使用 -source 7 或更高版本以启用 diamond 运算符)
- ERP商品管理业务逻辑封装(三十四)
- 【Alpha版本】冲刺阶段——Day2
- 3.修改更新源sources.list,提高软件下载安装速度(2017.04.05)
- 固态盘经常性蓝屏处理方法(WIN7/8)
热门文章
- python学习 0 python简介
- 我对KMP算法的理解
- 操作文件-取出一个60s内log日志中ip访问次数超过100次的ip
- python3(二十二) oop
- Linux c++ vim环境搭建系列(2)——Ubuntu18.04.4编译安装llvm clang
- 当Spring Cloud Alibaba Sentinel碰上Spring Cloud Sleuth会擦出怎样的火花
- 吾八哥学k8s(二):golang服务部署到kubernetes
- unity3d之简单动画
- 三个步骤就能让你轻松掌握Python爬虫
- Linux服务器压力测试总结(CPU、Memory、IO等)