2014
11-05

# Escape

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.

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.

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.

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

9
Bad luck!

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
#define cler(arr, val)    memset(arr, val, sizeof(arr))
#define IN     freopen ("in.txt" , "r" , stdin);
#define OUT  freopen ("out.txt" , "w" , stdout);
typedef long long  LL;
const int MAXN = 66666;//点数的最大值
const int MAXM = 20006;//边数的最大值
const int INF = 1101521204;
const int mod = 10000007;
int m,n,k,eng;
struct node
{
int x,y,v,t,f;
}kp[102];
struct node1
{
int x,y,step;
bool operator < (const node1 &a) const
{
return step>a.step;
}
};
priority_queue<node1>q;
int xx[5]={0,-1,1,0,0};
int yy[5]={0,0,0,-1,1};
bool vis[110][110][1009];
bool mp[110][110][1009];
bool point[110][110];
bool inmp(int x,int y)
{
if(x<0||x>m||y<0||y>n) return false;
return true;
}
int bfs(int x,int y)
{
node1 front,rear;
front.x=x,front.y=y,front.step=0;
while(!q.empty()) q.pop();
q.push(front);
while(!q.empty())
{
front=q.top();
front.step++;
q.pop();
for(int i=0;i<5;i++)
{
int dx=front.x+xx[i],dy=front.y+yy[i];
if(inmp(dx,dy)&&!mp[dx][dy][front.step]&&!point[dx][dy]&&!vis[dx][dy][front.step])
{
vis[dx][dy][front.step]=true;
if(dx==m&&dy==n) return front.step;//到达终点
if(front.step+1>eng) continue;
rear.x=dx,rear.y=dy,rear.step=front.step;
q.push(rear);
}
}
}
return -1;
}
int main()
{
// IN;
while(scanf("%d%d%d%d",&m,&n,&k,&eng)!=EOF)
{
cler(mp,false);
cler(vis,false);
cler(point,false);
for(int i=0;i<k;i++)
{
char c[3];
scanf("%s%d%d%d%d",c,&kp[i].t,&kp[i].v,&kp[i].x,&kp[i].y);
if(c[0]=='N') kp[i].f=1;
else if(c[0]=='S') kp[i].f=2;
else if(c[0]=='W') kp[i].f=3;
else if(c[0]=='E') kp[i].f=4;
point[kp[i].x][kp[i].y]=true;
}
for(int i=0;i<k;i++)
{
int dx=kp[i].x,dy=kp[i].y,v=kp[i].v,next=kp[i].f;
for(int j=1;j<=eng;j++)
{
int flag=0;
dx+=xx[next],dy+=yy[next];
if(!inmp(dx,dy)) break;
for(int l=0;l<v;l++)//路上有堡垒
{
if(point[dx-xx[next]*l][dy-yy[next]*l])
{
flag=1;break;
}
}
if(flag) break;
int x=j;
while(x<=eng)
{
mp[dx][dy][x]=true;//标记不能走
x+=kp[i].t;
}
}
}
int ans=bfs(0,0);
if(ans==-1)
}