HDU - 3533 Escape

英文原版题目

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

InputcopyOutputcopy
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
9
Bad luck!

翻译题目

HEU大学的同学们正在进行军训演习。
今天红蓝两军展开激战。蓝军发现小A是红军的间谍,于是小A必须从蓝军总部(0,0)逃往红军总部(m,n)。战场是一个m*n的矩形区域,蓝军和红军总部分别位于(0,0)和(m,n)。下图展示了战场布局和后续会用到的方向标识。
 



蓝军为了报复,决定在小A逃跑途中全力追杀。蓝军布置了许多炮台,这些炮台会定期朝固定方向射击。无论小A移动与否,每秒都会消耗1单位体力。如果体力耗尽或被子弹击中,任务就会失败。小A每秒可以向东南西北移动一格,也可以选择原地装死。
为简化问题,假设小A不能在半秒时停止移动。在移动过程中既不会被击中也不会阻挡子弹——子弹只能在整数坐标点击杀小A。举个栗子:子弹以每秒3格的速度从(0,3)射向(0,0),同时小A以每秒1格从(0,0)逃往(0,1),这时小A能活下来。但如果子弹速度改为每秒2格,小A就会在(0,1)领盒饭。
现在请判断小A能否成功逃亡。

输入

每个测试用例首行包含四个整数m,n,k,d(2<=m,n<=100, 0<=k<=100, m+n<=d<=1000),分别表示战场尺寸、炮台数量和初始体力值。随后k行描述炮台属性,每行包含一个方向字符c('N','S','E','W'),周期t,子弹速度v,以及炮台坐标(x,y)。注意:
1. 炮台被其他炮台击中时只会阻挡子弹而不会被摧毁
2. 两颗子弹相遇时会"擦肩而过"互不影响
3. 所有炮台在小A开始逃亡时同步开火
输入直到文件结束。

输出

若小A能成功逃亡,输出最短耗时(秒);否则输出"Bad luck!"(不含引号)。

样例

输入样例输出样例
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
9
Bad luck!

题目大意

一个人从(0,0)跑到(n,m),只有d点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y
问这个人能不能安全到达终点,如果可以输出最小的能量花费。
要求:
1.人不能到达炮塔所在的坐标
2.炮塔会挡住子弹
3.途中遇到子弹是安全的,但是人如果停在这个坐标,而子弹也刚好到这个坐标,人就被射死
4.人可以选择停止不动
输入第一行是四个整数n,m,k,d,n,m表示地图的大小,k表示有几个炮塔,d表示能量。
接下来k行每行有一个字符c,以及四个整数t,v,x,y。字符c表示炮塔发射子弹的方向,用E,S,W,N分别表示东南西北。t表示子弹的发射间隔,v表示子弹的速度,x,y是炮塔的坐标。

思路

基本的思路依旧是BFS,需要在BFS的过程中做一些处理。我们可以枚举走到某一点后的四个方向,每次从四个方向中选一个方向,沿着这个方向去寻找第一个炮塔(如果有的话),看这个的炮塔的子弹方向是不是朝向自己以及子弹在这个时刻会不会在这个点上。枚举完四个点后都发现不会有子弹打到自己时,就把这个点入队列。
对于判重,需要定义vis[x][y][time]表示点(x,y)在time时刻的访问状态,这里需要注意的是这个数组不能开int,只能开bool,虽然效果好像是一样的,但是开int会爆内存,因为int占4byte,而bool只占1byte。

代码与注释

#include<bits/stdc++.h>
using namespace std;
const int maxt=1000+10;
const int maxn2=100+5;
struct castle{       //碉堡节点 
	int t,v,x,y;     //时间间隔、速度、坐标 
	char c;
	castle(int t=0,int v=0,int x=0,int y=0,char c='N'):t(t),v(v),x(x),y(y),c(c){}
}cas[maxn2];
struct node{
	int x,y;
	int time;   //走到当前节点花了多少时间 
	node(int x=0,int y=0,int time=0):x(x),y(y),time(time){}
};
bool vis[maxt][maxn2][maxn2];  
bool remb[maxt][maxn2][maxn2]; //记录每个时刻那些位置有炮弹 
bool g[maxn2][maxn2];   //记录碉堡的坐标 
int m,n,k,d;
int dir[][2]={{1,0},{-1,0},{0,-1},{0,1},{0,0}};
bool isValid(node &nd){
	return nd.x>=0&&nd.x<=m && nd.y>=0&&nd.y<=n;
}
void solve(){
	memset(remb,false,sizeof(remb));	
	for(int i=0;i<k;i++){
		int x=0,y=0;
		switch(cas[i].c){
			case 'N': x=-1;break;
			case 'S': x=1;break;
			case 'W': y=-1;break;
			case 'E': y=1;
		}
		int x1,y1;
        for(int j=1;;j++){
        	x1=j*x+cas[i].x;   
        	y1=j*y+cas[i].y;
        	if(x1<0 || x1>m || y1<0 || y1>n)break;
        	if(g[x1][y1])break;            
        	if(j%cas[i].v==0){  //子弹会停留的坐标,时间=步数/速度 
        		for(int h=j/cas[i].v;h<=d;h+=cas[i].t){
        			remb[h][x1][y1]=true;
				}
			}
		}
  }
}
bool judge(node &nd){   //当逃到某点时,明显无法到终点时,剪枝 
	if(nd.time>d)return false;
	int temp=abs(nd.x-m)+abs(nd.y-n);
	return d-nd.time>=temp;
}
void bfs(){
	memset(vis,false,sizeof(vis));
	queue<node>q;
	q.push(node(0,0,0));
	vis[0][0][0]=true;
	while(!q.empty()){
		node u=q.front();
		q.pop();
		if(u.x==m && u.y==n){
			printf("%d\n",u.time);
			return ;
		}
		for(int i=0;i<5;i++){
			node v=node(u.x+dir[i][0],u.y+dir[i][1],u.time+1);
			if(isValid(v) && !vis[v.time][v.x][v.y] && !remb[v.time][v.x][v.y] && judge(v) && !g[v.x][v.y]){
				q.push(v);
				vis[v.time][v.x][v.y]=true;
			}
		}
	}
	printf("Bad luck!\n");
}
int main(){
	while(scanf("%d%d%d%d",&m,&n,&k,&d)==4){
		getchar();
		memset(g,false,sizeof(g));
		for(int i=0;i<k;i++){
			scanf("%c",&cas[i].c);
			scanf("%d%d%d%d",&cas[i].t,&cas[i].v,&cas[i].x,&cas[i].y);
			getchar();    
			g[cas[i].x][cas[i].y]=true;
		}
		solve();
		bfs();
	}
	return 0;
} 

 本篇题解到此结束!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值