题目链接:Escape HDU - 3533
===================================================
Escape
Time Limit: 1000MS
Memory Limit: 32768 kB
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!
===================================================
算法:A* BFS
思路:
函数 用步数 + 与终点的曼哈顿距离(因为只是四方向行进)
现在感觉做这些模拟题,读题真的是关键中的关键,很容易踩坑,然后纠结很久;
难点关键:首先,炮台会帮你挡子弹;然后二维坐标上存在时间维度,这里需要用到三维数组记录状态,因为子弹的原因,因为最糟糕情况就是你这一时间被一个子弹挡住去路,
然后下一时间你又被挡住去路,并且当前位置会被子弹打到,需要往回走;所以单纯二维不够用;三维能用的又一原因在于题目存在体力值(相当于时间,用完就跳出),所以不会存在死循环;
这里子弹模拟计算,又是另一难点。看了其他人代码,有些是走到当前位置再去判断是否会被四周打到。
然后另一种更优方法就是用3维数组去记录子弹的动作,发挥想象力,就像是每一个时间维度下的二维就是幻灯片。然后你记录这些子弹轨迹,这样理解就简单多了。
===================================================
#include <iostream>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
bool vis[102][102][1001],bullet[102][102][1001],ditu[102][102];
int m,n,k,d;
int xx[] = {0,0,-1,1,0};
int yy[] = {-1,1,0,0,0};
struct castle{
int c,v,t,x,y;
}ca[102];
struct node{
int x,y,step,f;
bool operator<(const node &a)const{
return f>a.f;
}
node(int a,int b,int c) {
x = a,y = b,step = c;
f = step + abs(x - m) + abs(y - n);
};
};
int checkD(char ch){
switch(ch){
case 'N':return 2;
case 'S':return 3;
case 'E':return 1;
case 'W':return 0;
}
return -1;
}
bool check(int x,int y){
return x>=0&&x<=m&&y>=0&&y<=n;
}
void init(){
memset(bullet,false,sizeof bullet);
for(int i=1;i<=k;i++){
int c = ca[i].c , v = ca[i].v,t = ca[i].t;
for(int j=1;;j++){
int x = ca[i].x + xx[c]*j , y = ca[i].y + yy[c]*j;
if(ditu[x][y]||!check(x,y)) break;
if(j%v==0){
for(int z=0;z+j/v<=d;z+=t){
//cout<<i<<" "<<x<<" "<<y<<" "<<z+j/v<<endl;
bullet[x][y][z+j/v] = true;
}
}
}
}
}
bool bfs(){
memset(vis,false,sizeof vis);
priority_queue<node> q;
q.push(node(0,0,0));
vis[0][0][0] = true;
while(!q.empty()){
node p = q.top();q.pop();
//cout<<p.x<<" "<<p.y<<" "<<p.step<<endl;
if(p.step>d) continue;
if(p.x==m&&p.y==n){cout<<p.step<<endl;return true;}
for(int i=0;i<5;i++){
int x = p.x + xx[i],y = p.y + yy[i] , step = p.step + 1;
if(!check(x,y)||ditu[x][y]||vis[x][y][step]||bullet[x][y][step]) continue;
vis[x][y][step] = true;
q.push(node(x,y,step));
}
}
return false;
}
int main()
{
while(cin>>m>>n>>k>>d){
memset(ditu,false,sizeof ditu);
char ch;int t,v,x,y;
for(int i=1;i<=k;i++){
cin>>ch>>t>>v>>x>>y;
ditu[x][y] = true;
ca[i].c = checkD(ch);
ca[i].t = t,ca[i].v = v , ca[i].x = x, ca[i].y = y;
}
//for(int i=1;i<=k;i++) cout<<ca[i].c<<" "<<ca[i].t<<" "<<ca[i].v<<" "<<ca[i].x<<" "<<ca[i].y<<endl;
init();
/*
for(int i=0;i<=d;i++){
for(int j=0;j<=m;j++){
for(int z=0;z<=n;z++) cout<<bullet[j][z][i]<<" ";
cout<<endl;
}
cout<<endl;
}
*/
if(!bfs()) puts("Bad luck!");
}
return 0;
}