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

Escape

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1792    Accepted Submission(s): 529

Problem Description
The students of the HEU are maneuvering for their military training.
The red army and the blue army are at war today. The blue army finds that Little A is the spy of the red army, so Little A has to escape from the headquarters of the blue army to that of the red army. The battle field is a rectangle of size m*n, and the headquarters
of the blue army and the red army are placed at (0, 0) and (m, n), respectively, which means that Little A will go from (0, 0) to (m, n). The picture below denotes the shape of the battle field and the notation of directions that we will use later.

The blue army is eager to revenge, so it tries its best to kill Little A during his escape. The blue army places many castles, which will shoot to a fixed direction periodically. It costs Little A one unit of energy per second, whether he moves or not. If he
uses up all his energy or gets shot at sometime, then he fails. Little A can move north, south, east or west, one unit per second. Note he may stay at times in order not to be shot.
To simplify the problem, let’s assume that Little A cannot stop in the middle of a second. He will neither get shot nor block the bullet during his move, which means that a bullet can only kill Little A at positions with integer coordinates. Consider the example
below. The bullet moves from (0, 3) to (0, 0) at the speed of 3 units per second, and Little A moves from (0, 0) to (0, 1) at the speed of 1 unit per second. Then Little A is not killed. But if the bullet moves 2 units per second in the above example, Little
A will be killed at (0, 1).
Now, please tell Little A whether he can escape.

 
Input
For every test case, the first line has four integers, m, n, k and d (2<=m, n<=100, 0<=k<=100, m+ n<=d<=1000). m and n are the size of the battle ground, k is the number of castles and d is the units of energy Little A initially has. The next k lines describe
the castles each. Each line contains a character c and four integers, t, v, x and y. Here c is ‘N’, ‘S’, ‘E’ or ‘W’ giving the direction to which the castle shoots, t is the period, v is the velocity of the bullets shot (i.e. units passed per second), and
(x, y) is the location of the castle. Here we suppose that if a castle is shot by other castles, it will block others’ shots but will NOT be destroyed. And two bullets will pass each other without affecting their directions and velocities.
All castles begin to shoot when Little A starts to escape.
Proceed to the end of file.
 
Output
If Little A can escape, print the minimum time required in seconds on a single line. Otherwise print “Bad luck!” without quotes.
 
Sample Input
4 4 3 10
N 1 1 1 1
W 1 1 3 2
W 2 1 2 4
4 4 3 10
N 1 1 1 1
W 1 1 3 2
W 1 1 2 4
 
Sample Output
9
Bad luck!
 
Source

题解:
题目要求:

1.直接把能量限制当成时间限制吧,在规定的时间内,从左上角到达右下角。

2.人可以上下左右走或不走, 每座城堡都有一支方向固定的枪,每支枪都有规定的速度和发射频率。

3.人只有在整数时刻下与子弹相遇才会被命中。

4.如果一座城堡被其他子弹射中,那颗子弹会被挡下。注意:即使不是在整数时刻射中,子弹也会被挡下。

做法:

1.开一个三维数组 hav[x][y][time],表明在time时刻, 坐标(x,y)上有无子弹。显然需要预处理这个数组。

2.使用BFS或者A*搜索

3.关于判重:一开始想到,由于子弹的进程是动态的,所以人可能为了回避子弹而往回走,所以就没有加vis判重(当初想到的是vis[x][y]的二维判重)。结果当然是不能通过了,后来看了下题解,开了个三维判重:vis[x][y][time],即增加了"时刻"这一维度。

问:那平常的的为什么开二维判重(vis[x][y])就够了呢,如POJ1077 八数码?

答:因为平常的题目,不用往回走。当第一次被访问的时候,这个位置被访问的最早时间一定是当前时间,之后的访问时间都不可能小于它,这个最早时间对于题目来说是最优 的,所以不用加上“时间”维度。而此题可以往回走,有可能出现最早时刻到达了这个位置,而下一时刻不管怎么走,都会被射死,反而是第二早访问时间才可以逃过一劫。因此这个最早访问时间对于题目来说不一定是最优的,所以此题要加上“时间”维度来判重。

BFS:

Accepted 3533 764MS 25768K 2698 B

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; bool M[MAXN][MAXN], hav[MAXN][MAXN][];
bool vis[MAXN][MAXN][]; int dir[][] = {-,, ,, ,, ,-, ,};
int n, m, k, d; struct
{
int dir, x, y, t,v;
}ca[MAXN]; struct node
{
int x, y, step;
}; void pre_set()
{
ms(hav, );
for(int i = ; i<k; i++) //枚举城堡
{
for(int j = ; j<=d; j += ca[i].t) //模拟一颗子弹
{
for(int k = ; ; k++) //枚举路程
{
int x = ca[i].x + dir[ca[i].dir][]*k;
int y = ca[i].y + dir[ca[i].dir][]*k;
if(x< || x>n || y< ||y>m || M[x][y]) break;
if(k%ca[i].v==) //到达整点时刻,更新hav数组
hav[x][y][j+k/ca[i].v] = true;
}
}
}
} queue<node>que;
int bfs()
{
ms(vis ,);
while(!que.empty()) que.pop(); node now, tmp;
now.x = now.y = ;
now.step = ;
vis[][][] = true;
que.push(now); while(!que.empty())
{
now = que.front();
que.pop(); if(now.step>d) //累死了
return -;
if(now.x==n && now.y==m) //顺利回营
return now.step; for(int i = ; i<; i++)
{
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
tmp.step = now.step + ;
if(tmp.x>= && tmp.x<=n && tmp.y>= && tmp.y<=m && !M[tmp.x][tmp.y]
&& !hav[tmp.x][tmp.y][tmp.step] && !vis[tmp.x][tmp.y][tmp.step])
{
vis[tmp.x][tmp.y][tmp.step] = ;
que.push(tmp);
}
}
}
return -;
} int main()
{
while(scanf("%d%d%d%d",&n,&m,&k,&d)!=EOF)
{
ms(M, );
char dir;
for(int i = ; i<k; i++)
{
getchar();
scanf("%c%d%d%d%d",&dir, &ca[i].t, &ca[i].v, &ca[i].x, &ca[i].y);
if(dir=='N') ca[i].dir = ;
if(dir=='S') ca[i].dir = ;
if(dir=='E') ca[i].dir = ;
if(dir=='W') ca[i].dir = ;
M[ca[i].x][ca[i].y] = ;
} pre_set();
int ans = bfs();
if(ans==-)
puts("Bad luck!");
else
printf("%d\n", ans);
}
}

A*:

Accepted 3533 374MS 26092K 2861 B

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int MOD = 1e9+;
const int MAXN = +; bool M[MAXN][MAXN], hav[MAXN][MAXN][];
bool vis[MAXN][MAXN][]; int dir[][] = {-,, ,, ,, ,-, ,};
int n, m, k, d; struct
{
int dir, x, y, t,v;
}ca[MAXN]; struct node
{
int x, y, f, g, h; //g即为step
bool operator<(const node &a)const{
return f>a.f;
}
}; void pre_set()
{
ms(hav, );
for(int i = ; i<k; i++) //枚举城堡
{
for(int j = ; j<=d; j += ca[i].t) //模拟一颗子弹
{
for(int k = ; ; k++) //枚举路程
{
int x = ca[i].x + dir[ca[i].dir][]*k;
int y = ca[i].y + dir[ca[i].dir][]*k;
if(x< || x>n || y< ||y>m || M[x][y]) break;
if(k%ca[i].v==) //到达整点时刻,更新hav数组
hav[x][y][j+k/ca[i].v] = true;
}
}
}
} priority_queue<node>que;
int bfs()
{
ms(vis ,);
while(!que.empty()) que.pop(); node now, tmp;
now.x = now.y = ;
now.g = ;
vis[][][] = true;
que.push(now); while(!que.empty())
{
now = que.top();
que.pop(); if(now.g>d) //累死了
return -;
if(now.x==n && now.y==m) //顺利回营
return now.g; for(int i = ; i<; i++)
{
tmp.x = now.x + dir[i][];
tmp.y = now.y + dir[i][];
tmp.g = now.g + ;
if(tmp.x>= && tmp.x<=n && tmp.y>= && tmp.y<=m && !M[tmp.x][tmp.y]
&& !hav[tmp.x][tmp.y][tmp.g] && !vis[tmp.x][tmp.y][tmp.g])
{
vis[tmp.x][tmp.y][tmp.g] = ;
tmp.h = abs(tmp.x-n) + abs(tmp.y-m);
tmp.f = tmp.g + tmp.h;
que.push(tmp);
}
}
}
return -;
} int main()
{
while(scanf("%d%d%d%d",&n,&m,&k,&d)!=EOF)
{
ms(M, );
char dir;
for(int i = ; i<k; i++)
{
getchar();
scanf("%c%d%d%d%d",&dir, &ca[i].t, &ca[i].v, &ca[i].x, &ca[i].y);
if(dir=='N') ca[i].dir = ;
if(dir=='S') ca[i].dir = ;
if(dir=='E') ca[i].dir = ;
if(dir=='W') ca[i].dir = ;
M[ca[i].x][ca[i].y] = ;
} pre_set();
int ans = bfs();
if(ans==-)
puts("Bad luck!");
else
printf("%d\n", ans);
}
}

HDU3533 Escape —— BFS / A*算法 + 预处理的更多相关文章

  1. HDU 3533 Escape (BFS + 预处理)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  2. poj 3501 Escape from Enemy Territory 预处理+二分+bfs

    传送门 给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标. 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离. 首先预处理出来地图上的所有点到敌人的最短距离, ...

  3. 【算法系列学习】[kuangbin带你飞]专题二 搜索进阶 D - Escape (BFS)

    Escape 参考:http://blog.csdn.net/libin56842/article/details/41909459 [题意]: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消 ...

  4. 【搜索】 HDU 3533 Escape BFS 预处理

    要从0,0 点 跑到m,n点  路上会有k个堡垒发射子弹.有子弹的地方不能走,子弹打到别的堡垒就会消失,或者一直飞出边界(人不能经过堡垒 能够上下左右或者站着不动 每步都须要消耗能量  一共同拥有en ...

  5. HDU3533 Escape

    题目: The students of the HEU are maneuvering for their military training. The red army and the blue a ...

  6. BFS/DFS算法介绍与实现(转)

    广度优先搜索(Breadth-First-Search)和深度优先搜索(Deep-First-Search)是搜索策略中最经常用到的两种方法,特别常用于图的搜索.其中有很多的算法都用到了这两种思想,比 ...

  7. 图论——读书笔记(基于BFS广度优先算法的广度优先树)

    广度优先树 对于一个图G=(V,E)在跑过BFS算法的过程中会创建一棵广度优先树. 形式化一点的表示该广度 优先树的形成过程是这样的: 对于图G=(V,E)是有向图或是无向图, 和图中的源结点s, 我 ...

  8. HDU 3533 Escape(bfs)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. (原创)BFS广度优先算法,看完这篇就够了

    BFS算法 上一篇文章讲解了DFS深度优先遍历的算法,我们说 DFS 顾名思义DEEPTH FIRET,以深度为第一标准来查找,以不撞南墙不回头的态度来发掘每一个点,这个算法思想get到了其实蛮简单. ...

随机推荐

  1. VMware VMnet8 模式共享主机网络配置静态 IP 和 DNS

    一.简介 NAT网络模式: 1. 宿主机可以看做一个路由器,虚拟机通过宿主机的网络来访问  Internet: 2. 可以安装多台虚拟机,组成一个小型局域网,例如:搭建 hadoop 集群.分布式服务 ...

  2. 程序自动分析(codevs 4600)

    题目描述 Description 在实现程序自动分析的过程中,常常需要判定一些约束条件是否能被同时满足. 考虑一个约束满足问题的简化版本:假设x1,x2,x3,…代表程序中出现的变量,给定n个形如xi ...

  3. mysql报错Packet for query is too large (12238 > 1024). You can change this value

    今天将项目部署到linux服务器的时候莫名其妙的报一些错误,可是在本地啥错没有,通过实时查看tomcat 的日志之后发现报错是: 实时查看日志: .先切换到:cd usr/local/tomcat5/ ...

  4. Linux 下MySQL 安装与卸载

    这个写的比较好:http://www.cnblogs.com/starof/p/4680083.html 2.卸载系统自带的Mariadb rpm -qa|grep mariadb         / ...

  5. LeetCode OJ--Search in Rotated Sorted Array II

    http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 如果在数组中有重复的元素,则不一定说必定前面或者后面的一半是有序的 ...

  6. Struts2的验证框架简单吗?

    Struts2验证框架是基于Struts拦截器开发的,具有良好的扩展性:一般的验证都可以支持.现在我们以一个注册验证的例子进行总结: 新建一个model,User: public class User ...

  7. easyui分页时,总页数出错

    错误出现 MyBatis用easyui写后台分页代码时,出现翻页后显示总页数错误 代码如下 可能原因在于后台mappers.xml里的sql语句错误 <select id="getPr ...

  8. Spark学习(三): 基本架构及原理

    Apache Spark是一个围绕速度.易用性和复杂分析构建的大数据处理框架,最初在2009年由加州大学伯克利分校的AMPLab开发,并于2010年成为Apache的开源项目之一,与Hadoop和St ...

  9. (转) go Cron的使用

    Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式: Seconds Minutes Hours DayofMonth Month ...

  10. iOS release版本去除NSLog打印信息

    因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉. 我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时 ...