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!
这道题主要读清题
1>逃生者不能到达堡垒的地方
2>子弹在整秒到达整数点才会打死逃生者
3>堡垒能抵挡背的堡垒发来的 子弹
预处理每个点会有哪几个堡垒发来的子弹,记录第一个子弹到来的时间以及周期,然后再bfs,虽然有点烦,但是细心 点还是可以的
wa了几发是因为vector空间没清空和有一个地方写错了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
using namespace std;
typedef long long LL;
#define pb push_back
#define mk make_pair
#define fi first
#define se second
const int inf = 0x3f3f3f3f;
bool vis[105][105][1005];
int col[5] = {0,0,-1,1};
int con[5] = {-1,1,0,0};
int n,m,k,d;
bool flag[105][105];
vector<pair<int,int> > mp[105][105];
typedef struct Node{
char ch;
int x,y;
int t,v;
}Node;
Node node[105];
bool judge(int i,int j)
{
if(i < 0 || j < 0)
return false;
if(i > n || j > m)
return false;
return true;
}
void init()
{
for(int i = 0;i <= 100;++i)
for(int j = 0;j <= 100;++j)
mp[i][j].clear();
for(int i = 0;i < k;++i)
{
if(node[i].v == 0) continue;
if(node[i].ch == 'E'){
for(int j = node[i].y + 1;j <= m;++j){
if(flag[node[i].x][j]) break;
if((j - node[i].y) % node[i].v == 0){
mp[node[i].x][j].pb(mk((j - node[i].y) / node[i].v,node[i].t));
}
}
}else if(node[i].ch == 'W'){
for(int j = node[i].y - 1;j >= 0;--j){
if(flag[node[i].x][j]) break;
if((node[i].y - j) % node[i].v == 0){
mp[node[i].x][j].pb(mk((node[i].y - j) / node[i].v,node[i].t));
}
}
}else if(node[i].ch == 'N'){
for(int j = node[i].x - 1;j >= 0;--j){
if(flag[j][node[i].y]) break;
if((node[i].x - j) % node[i].v == 0){
mp[j][node[i].y].pb(mk((node[i].x - j) / node[i].v,node[i].t));
}
}
}else if(node[i].ch == 'S'){
for(int j = node[i].x + 1;j <= n;++j){
if(flag[j][node[i].y]) break;
if((j - node[i].x) % node[i].v == 0){
mp[j][node[i].y].pb(mk((j - node[i].x) / node[i].v,node[i].t));
}
}
}
}
}
typedef struct dog{
int x,y;
int t;
}dog;
int bfs()
{
memset(vis,false,sizeof(vis));
queue<dog>que;
vis[0][0][0] = true;
que.push((dog){0,0,0});
while(!que.empty())
{
dog tmp = que.front();
que.pop();
if(tmp.t > d)
continue;
int x = tmp.x,y = tmp.y;
if(x == n && y == m){
return tmp.t;
}
for(int i = 0;i < 4;++i)
{
int x1 = x + col[i],y1 = y + con[i];
if(judge(x1,y1) && !vis[x1][y1][tmp.t + 1] && !flag[x1][y1]){
vis[x1][y1][tmp.t + 1] = true;
bool ptr = false;
for(int j = 0;j < mp[x1][y1].size();++j)
{
if((tmp.t + 1 - mp[x1][y1][j].fi) % mp[x1][y1][j].se == 0){
vis[x][y][tmp.t + 1] = true;
ptr = true;
que.push((dog){x,y,tmp.t + 1});
break;
}
}
if(!ptr){
que.push((dog){x1,y1,tmp.t + 1});
}
}
}
}
return -1;
}
int main()
{
while(~scanf("%d %d %d %d",&n,&m,&k,&d))
{
memset(flag,false,sizeof(flag));
for(int i = 0;i < k;++i)
{
char ch;
int t,v,x,y;
scanf(" %c %d %d %d %d",&ch,&t,&v,&x,&y);
flag[x][y] = true;
node[i] = (Node){ch,x,y,t,v};
}
init();
int num = bfs();
if(num == -1){
printf("Bad luck!\n");
}else{
printf("%d\n",num);
}
}
return 0;
}