Escape

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

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
 
題意:
一個人從(0,0)跑到(n,m),只有k點能量,一秒消耗一點,在圖中有k個炮塔,給出炮塔的射擊方向c,射擊間隔t,子彈速度v,座標x,y
問這個人能不能安全到達終點
要求: 
1.人不能到達炮塔所在的座標
2.炮塔會擋住子彈
3.途中遇到子彈是安全的,但是人如果停在這個座標,而子彈也剛好到這個座標,人就被射死
4.人可以選擇停止不動
 
思路:其實不難,我們只需要看當人位於某個點的時候,其四個方向是否有炮塔,這個炮塔是都向人的方向射擊,然後再看子彈是否剛好位於這個座標即可。
而標記的話,vis[x][y][time],對於time時刻,人位於x,y的情況只需要訪問一次,這是唯一的
我的程式碼:
/*************************************************************************
> File Name: hdu_3533.cpp
> Author: Howe_Young
> Mail: [email protected]
> Created Time: 2015年04月28日 星期二 19時14分24秒
************************************************************************/ #include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue> using namespace std;
const int maxn = ;
const int Next[][] = {, , , , , -, -, , , };//可以四個方向,也可以呆在原地不動
int n, m, k, d;
struct castle{//碉堡
char dir;//炮塔方向
int t, v;//t是週期,v是速度
};
struct Node{
int x, y, step;
};
castle bullet[maxn][maxn];
bool vis[maxn][maxn][];
bool check(Node node)
{
return (node.x < || node.y < || node.x > m || node.y > n);
}
void bfs()
{
memset(vis, false, sizeof(vis));
queue<Node> Q;
Node cur, next;
cur.x = cur.y = cur.step = ;
vis[][][] = true;
Q.push(cur);
while (!Q.empty())
{
bool flag;
cur = Q.front();
Q.pop();
if (cur.step > d)
break;
if (cur.x == m && cur.y == n)
{
printf("%d\n", cur.step);
return;
}
for (int i = ; i < ; i++)
{
next.x = cur.x + Next[i][];
next.y = cur.y + Next[i][];
next.step = cur.step + ;
if (check(next))
continue;
//這裡前面那個是判斷此點是否有碉堡,如果有碉堡的話不能走
if (bullet[next.x][next.y].t == && !vis[next.x][next.y][next.step] && next.step <= d)
{
flag = true;
for (int j = next.x - ; j >= ; j--)//向上找有沒有碉堡
{
if (bullet[j][next.y].t != && bullet[j][next.y].dir == 'S')//說明有碉堡.並且朝南
{
int dis = next.x - j;//碉堡與人的距離
if (dis % bullet[j][next.y].v != )//如果不能整除的話,說明子彈在這個點時肯定不是整點,所以直接跳過
break;
int tmp = next.step - dis / bullet[j][next.y].v;//人走的總時間減去第一顆子彈到這需要多少時間
if (tmp < )//如果人到這,子彈還到不了,所以安全,直接跳過
break;
if (tmp % bullet[j][next.y].t == )//如果子彈正好到這,這時人就被打死了
{
flag = false;
break;
}
}
if (bullet[j][next.y].t != )//如果炮塔不朝南的話就直接擋住子彈了
break;
}
if (!flag)
continue;
//下面其他三個方向同理
for (int j = next.x + ; j <= m; j++)
{
if (bullet[j][next.y].t != && bullet[j][next.y].dir == 'N')
{
int dis = j - next.x;
if (dis % bullet[j][next.y].v != )
break;
int tmp = next.step - dis / bullet[j][next.y].v;
if (tmp < )
break;
if (tmp % bullet[j][next.y].t == )
{
flag = false;
break;
}
}
if (bullet[j][next.y].t != )
break;
}
if (!flag)
continue;
for (int j = next.y - ; j >= ; j--)
{
if (bullet[next.x][j].t != && bullet[next.x][j].dir == 'E')
{
int dis = next.y - j;
if (dis % bullet[next.x][j].v != )
break;
int tmp = next.step - dis / bullet[next.x][j].v;
if (tmp < )
break;
if (tmp % bullet[next.x][j].t == )
{
flag = false;
break;
}
}
if (bullet[next.x][j].t != )
break;
}
if (!flag)
continue;
for (int j = next.y + ; j <= n; j++)
{
if (bullet[next.x][j].t != && bullet[next.x][j].dir == 'W')
{
int dis = j - next.y;
if (dis % bullet[next.x][j].v != )
break;
int tmp = next.step - dis / bullet[next.x][j].v;
if (tmp < )
break;
if (tmp % bullet[next.x][j].t == )
{
flag = false;
break;
}
}
if (bullet[next.x][j].t != )
break;
}
if (!flag)
continue;
vis[next.x][next.y][next.step] = true;
Q.push(next);
}
}
}
printf("Bad luck!\n");
}
int main()
{
while (~scanf("%d %d %d %d", &m, &n, &k, &d))
{
char ch;
int a, b, c, d;
memset(bullet, , sizeof(bullet));
for (int i = ; i < k; i++)
{
cin >> ch >> a >> b >> c >> d;
bullet[c][d].dir = ch;
bullet[c][d].t = a;
bullet[c][d].v = b;
}
bfs();
}
return ;
}