Escape
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2597 Accepted Submission(s): 754
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
2010 ACM-ICPC Multi-University Training Contest(10)——Host by HEU
Recommend
zhouzeyong
题意:
小A要从(0,0)点逃到(m,n)点 。有k个敌人碉堡在坐标上。小A最开始有d点能量,每过一秒会耗费一点能量。如果能量耗尽,小A就无法逃出升天。每个碉堡都会向某个方向(东西南北)发射一枚子弹,速度为v,发射周期为t。但是子弹会被碉堡所挡住。且子弹不会相撞。
子弹只能在整数坐标处击中小A。小A不会在非整数坐标下停下(但可以在整数坐标停下)。问在不被击中且能量不耗尽的情况下,最快什么时候能到达(m,n)点。
要注意:
1:小A不能走到敌人碉堡处。
2:终点可能会有碉堡
3:起点如果有碉堡的话,不影响出发。但是如果起点处有碉堡的话,不能在起点停留。
用vis[x][y][t]数组来标记
代表在t时刻到达(x,y)点。且这种情况是不能出现两次的。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define LL long long
#define M(a,b) memset(a,b,sizeof(a))
const int MAXN = 105;
const int INF = 0x3f3f3f3f;
int m,n,k,d,flag;
char c;
int t,v,x,y;
int X[6] = {0,0,1,-1,0};///五个方向是上下左右和停止不动
int Y[6] = {-1,1,0,0,0};
struct Node1
{
int id;///记录方向
int per,speed;
} castle[MAXN][MAXN];
bool vis[MAXN][MAXN][1005];///bool 型变量可节约内存
struct Node2
{
int x,y,t;
};
void init()
{
M(castle,0);
M(vis,false);
}
int bfs(int x,int y,int t)
{
queue<Node2> q;
Node2 temp;
temp = {x,y,t};
vis[0][0][0] = true;
q.push(temp);
while(!q.empty())
{
int x1 = q.front().x;
int y1 = q.front().y;
int t1 = q.front().t;
q.pop();
if(t1>d) break;///耗尽能量
if(x1==m&&y1==n)///到达终点
{
return t1;
}
for(int i=0; i<5; i++)///遍历五个方向,包括停止不动
{
int xx = x1+X[i];
int yy = y1+Y[i];
int tt = t1+1;
if(xx<0||xx>m||yy<0||yy>n||castle[xx][yy].id !=0||vis[xx][yy][tt]==true||tt>d)///坐标不越界,且没有敌人碉堡,且没被访问过,且剩余能量充足
continue;
int flag2 =1;
for(int j=xx+1; j<=m; j++)///向右边找
{
if(castle[j][yy].id!=0)///找到一个碉堡
{
if(castle[j][yy].id!=1)///碉堡方向开火向左
{
break;
}
else
{
if((j-xx)%castle[j][yy].speed!=0)///是否会出现子弹(整数点)
{
break;
}
int temp = (j-xx)/castle[j][yy].speed;///算出子弹到达的时间
if(tt-temp<0)break;
if((tt-temp)%castle[j][yy].per==0)///会被击中
{
flag2 = 0;
}
}
break;
}
}
if(flag2==0)///如果该点会被击中
{
continue;
}
for(int j=xx-1; j>=0; j--)///向左找
{
if(castle[j][yy].id!=0)
{
if(castle[j][yy].id!=2)
{
break;
}
else
{
if((xx-j)%castle[j][yy].speed!=0)
{
break;
}
int temp = (xx-j)/castle[j][yy].speed;
if(tt-temp<0)break;
if((tt-temp)%castle[j][yy].per==0)
{
flag2 = 0;
}
}
break;
}
}
if(flag2==0)
{
continue;
}
for(int j=yy+1; j<=n; j++)///向上找
{
if(castle[xx][j].id!=0)
{
if(castle[xx][j].id!=3)
{
break;
}
else
{
if((j-yy)%castle[xx][j].speed!=0)
{
break;
}
int temp = (j-yy)/castle[xx][j].speed;
if(tt-temp<0)break;
if((tt-temp)%castle[xx][j].per==0)
{
flag2 = 0;
}
}
break;
}
}
if(flag2==0)
{
continue;
}
for(int j=yy-1; j>=0; j--)///向下找
{
if(castle[xx][j].id!=0)
{
if(castle[xx][j].id !=4)
{
break;
}
else
{
if((yy-j)%castle[xx][j].speed!=0)
{
break;
}
int temp = (yy-j)/castle[xx][j].speed;
if(tt-temp<0)break;
if((tt-temp)%castle[xx][j].per==0)
{
flag2 = 0;
}
}
break;
}
}
if(flag2==0)///如果被击中,则放弃该点
{
continue;
}
else
{
vis[xx][yy][tt] = true;
Node2 temp2 = {xx,yy,tt};
q.push(temp2);
}
}
}
return -1;
}
int main()
{
while(~scanf("%d %d %d %d",&m,&n,&k,&d))
{
init();
for(int i=0; i<k; i++)
{
getchar();
scanf("%c %d %d %d %d",&c,&t,&v,&x,&y);///NSWE
if(c=='N')
{
castle[x][y] = {1,t,v};
}
else if(c=='S')
{
castle[x][y] = {2,t,v};
}
else if(c=='W')
{
castle[x][y] = {3,t,v};
}
else if(c=='E')
{
castle[x][y] = {4,t,v};
}
}
int ans = bfs(0,0,0);
if(ans==-1)
{
printf("Bad luck!\n");
}
else
{
printf("%d\n",ans);
}
}
return 0;
}