HDU 1253 胜利大逃亡 (BFS,剪枝)

2024-08-23 11:18
文章标签 剪枝 hdu 1253 bfs 逃亡 胜利

本文主要是介绍HDU 1253 胜利大逃亡 (BFS,剪枝),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

http://acm.hdu.edu.cn/showproblem.php?pid=1253

胜利大逃亡

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24270    Accepted Submission(s): 9300


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

Author
Ignatius.L

Recommend
Ignatius.L   |   We have carefully selected several similar problems for you:   1372  1072  1240  1175  1241 

显然的BFS,如果不加任何优化并且写得很搓的话估计会超时,注意常数优化(传引用,加inline)的话可能会1800ms左右,输入优化的话可能会1000ms以内,加上剪枝我优化到93ms,这里的剪枝用到了A*的思想,利用Manhattan距离可以剔除大量不比较搜索的点

#include<cstdio>
#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<ctime>
#include<cctype>
#include<cmath>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<map>
#include<set>
#define sqr(x) ((x)*(x))
#define LL long long
#define itn int
#define INF 0x3f3f3f3f
#define PI 3.1415926535897932384626
#define eps 1e-10
#define mmusing namespace std;itn A,B,C,T,MAP[55][55][55];
int dx[]={1,-1,0,0,0,0};
itn dy[]={0,0,1,-1,0,0};
itn dz[]={0,0,0,0,1,-1};struct node
{int x,y,z,t;void Set(int X=0,int Y=0,int Z=0,int T=0){x=X;y=Y;z=Z;t=T;}int MANHATTAN_DIS(){return abs(x-A)+abs(y-B)+abs(z-C);}
}q[200000];
int f,r;inline int ReadInt()
{int flag=0;char ch = getchar();int data = 0;while (ch < '0' || ch > '9'){if(ch=='-') flag=1;ch = getchar();}do{data = data*10 + ch-'0';ch = getchar();}while (ch >= '0' && ch <= '9');if(flag) data=-data;return data;
}inline bool check(int &x,int &y,itn &z)
{return x>=1 && x<=A && y>=1 && y<=B && z>=1 && z<=C;
}int bfs()
{int x,y,z;node now;q[f=r=0].x=q[f].y=q[f].z=1;q[f].t=0;MAP[1][1][1]=1;while (f<=r){now=q[f];f++;if (now.x==A && now.y==B && now.z==C) return now.t;if (now.MANHATTAN_DIS()+now.t>T)    continue;//用Manhattan距离剪枝if (now.t>T)    return -1;for (int i=0;i<6;i++){x=now.x+dx[i];y=now.y+dy[i];z=now.z+dz[i];if (check(x,y,z) && MAP[x][y][z]==0){q[++r].x=x;q[r].y=y;q[r].z=z;q[r].t=now.t+1;MAP[x][y][z]=1;}}}return -1;
}int main()
{#ifndef ONLINE_JUDGEfreopen("/home/fcbruce/文档/code/t","r",stdin);#endif // ONLINE_JUDGEint T_T;T_T=ReadInt();while (T_T--){A=ReadInt();B=ReadInt();C=ReadInt();T=ReadInt();for (itn i=1;i<=A;i++){for (itn j=1;j<=B;j++){for (int k=1;k<=C;k++){MAP[i][j][k]=ReadInt();}}}printf("%d\n",bfs());}return 0;
}


这篇关于HDU 1253 胜利大逃亡 (BFS,剪枝)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1099224

相关文章

hdu1254(嵌套bfs,两次bfs)

/*第一次做这种题感觉很有压力,思路还是有点混乱,总是wa,改了好多次才ac的思路:把箱子的移动当做第一层bfs,队列节点要用到当前箱子坐标(x,y),走的次数step,当前人的weizhi(man_x,man_y),要判断人能否将箱子推到某点时要嵌套第二层bfs(人的移动);代码如下:

usaco 1.3 Prime Cryptarithm(简单哈希表暴搜剪枝)

思路: 1. 用一个 hash[ ] 数组存放输入的数字,令 hash[ tmp ]=1 。 2. 一个自定义函数 check( ) ,检查各位是否为输入的数字。 3. 暴搜。第一行数从 100到999,第二行数从 10到99。 4. 剪枝。 代码: /*ID: who jayLANG: C++TASK: crypt1*/#include<stdio.h>bool h

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

hdu 1754 I Hate It(线段树,单点更新,区间最值)

题意是求一个线段中的最大数。 线段树的模板题,试用了一下交大的模板。效率有点略低。 代码: #include <stdio.h>#include <string.h>#define TREE_SIZE (1 << (20))//const int TREE_SIZE = 200000 + 10;int max(int a, int b){return a > b ? a :

hdu 1166 敌兵布阵(树状数组 or 线段树)

题意是求一个线段的和,在线段上可以进行加减的修改。 树状数组的模板题。 代码: #include <stdio.h>#include <string.h>const int maxn = 50000 + 1;int c[maxn];int n;int lowbit(int x){return x & -x;}void add(int x, int num){while

hdu 3790 (单源最短路dijkstra)

题意: 每条边都有长度d 和花费p,给你起点s 终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。 解析: 考察对dijkstra的理解。 代码: #include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <cstrin

hdu 2489 (dfs枚举 + prim)

题意: 对于一棵顶点和边都有权值的树,使用下面的等式来计算Ratio 给定一个n 个顶点的完全图及它所有顶点和边的权值,找到一个该图含有m 个顶点的子图,并且让这个子图的Ratio 值在所有m 个顶点的树中最小。 解析: 因为数据量不大,先用dfs枚举搭配出m个子节点,算出点和,然后套个prim算出边和,每次比较大小即可。 dfs没有写好,A的老泪纵横。 错在把index在d