HDU3533(Escape)
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!
思路
每个堡垒射出的子弹是周期性的,整数时刻下的整数坐标位置是不能到达的,所以针对每个堡垒进行预处理,预处理出时刻d内的子弹的路径轨迹,对不能走的点进行标记。而且堡垒射到堡垒会产生子弹无法穿越的情况也就是子弹打到堡垒就不用预处理后面的轨迹了。然后上BFS 或者 A*对visited[x][y][t] == true的点不能经过。
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
const int maxn = 105;
bool map[maxn][maxn];
bool visited[maxn][maxn][1005];
bool t[maxn][maxn][1005];
int m,n,k,d;
int dx[] = {-1,1,0,0,0};
int dy[] = {0,0,-1,1,0};
struct point{
int x,y,t,v,dir;
}s[maxn];
struct Node{
int x,y;
int f,g,h;
bool operator<(const Node a)const{
if(a.f == f){
return a.g < g;
}
else{
return a.f < f;
}
}
};
priority_queue<Node>q;
void clear_set()
{
memset(map,false,sizeof(map));
memset(visited,false,sizeof(visited));
memset(t,false,sizeof(t));
while(!q.empty()){
q.pop();
}
}
void pre_set()
{
for(int i = 0;i < k;i++){ //城堡数
for(int j = 0;j <= d;j += s[i].t){ //时间
int p = 1;
while(true){ //模拟一下子弹,子弹是有周期射出的。
int x = s[i].x + dx[s[i].dir]*p;
int y = s[i].y + dy[s[i].dir]*p;
if(x < 0 || x > m || y < 0 || y > n || map[x][y]){
break;
}
if(p % s[i].v == 0){
t[x][y][j+p/s[i].v] = true;
}
p++;
}
}
}
}
int Astar()
{
while(!q.empty()){
Node ptr = q.top(),p;
q.pop();
if(ptr.g > d){
return -1;
}
if(ptr.x == m && ptr.y == n){
return ptr.g;
}
for(int i = 0;i < 5;i++){
p.x = ptr.x + dx[i];
p.y = ptr.y + dy[i];
p.g = ptr.g + 1;
if(p.x < 0 || p.x > m || p.y < 0 || p.y > n){
continue;
}
if(map[p.x][p.y] || visited[p.x][p.y][p.g] || t[p.x][p.y][p.g]){
continue;
}
p.h = abs(p.x - m) + abs(p.y - n);
p.f = p.g + p.h;
visited[p.x][p.y][p.g] = true;
q.push(p);
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d%d",&m,&n,&k,&d)){
clear_set();
for(int i = 0;i < k;i++){
char c[5];
scanf("%s%d%d%d%d",c,&s[i].t,&s[i].v,&s[i].x,&s[i].y);
if(c[0] == 'N') s[i].dir = 0;
if(c[0] == 'S') s[i].dir = 1;
if(c[0] == 'W') s[i].dir = 2;
if(c[0] == 'E') s[i].dir = 3;
map[s[i].x][s[i].y] = true;
}
pre_set();
Node p;
p.x = 0;p.y = 0;p.g = 0;
p.h = m+n;p.f = p.h+p.g;
visited[0][0][0] = true;
q.push(p);
int ans = Astar();
if(ans == -1){
printf("Bad luck!\n");
}
else{
printf("%d\n",ans);
}
}
return 0;
}
愿你走出半生,归来仍是少年~
上一篇: HDU 3533 Escape
下一篇: HDU 3533 Escape
推荐阅读
-
JSP页面中文传递参数使用escape编码
-
C#版的 Escape() 和 Unescape() 函数分享
-
PHP实现Javascript中的escape及unescape函数代码分享
-
php escape URL编码
-
php mysql_real_escape_string函数用法与实例教程
-
escape unescape的php下的实现方法
-
区分escape、encodeURI和encodeURIComponent
-
PHP函数addslashes和mysql_real_escape_string的区别
-
PHP解密Unicode及Escape加密字符串
-
python对html代码进行escape编码的方法