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.

Escape



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!

我认为这个和八数码一样值得去学习,要考虑的因素真的非常多,记得用bool数组进行标记,不然处理不好剪枝就超时,开一个三维数组来标记时间,因为每个时间到达的地点只有一个,然后你还需要判断炮台射击的方向,需要注意的是

1,炮台不能过,子弹遇到炮台也不能过。
2,人只有在整数时刻与子弹相遇才会被打到。

#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
#define ads(x) (x<0?-x:x)
int n,m,k,l;
int step[5][2] = {0,1,1,0,0,-1,-1,0,0,0};
int map[105][105];
bool book[105][105][1005];
struct period
{
    char c;
    int t,v;
} s[105][105];
struct node
{
    int x,y,step;
};
int check(int x,int y)
{
    if(x<0 || x>n || y<0 || y>m)
        return 1;
    return 0;
}
void bfs()
{
    node a,next;
    queue<node> Q;
    int i,j,flag,dis,tem;
    a.x = a.y = a.step = 0;
    Q.push(a);
    book[0][0][0]=1;
    while(!Q.empty())
    {
        a = Q.front();
        Q.pop();
        if(a.step>l)
            break;
        if(a.x == n && a.y == m)
        {
            printf("%d\n",a.step);
            return ;
        }
        for(i = 0; i<5; i++)
        {
            next = a;
            next.x+=step[i][0];
            next.y+=step[i][1];
            next.step++;
            if(check(next.x,next.y)) continue;
            if(!s[next.x][next.y].t && !book[next.x][next.y][next.step] && next.step<=l)
            {
                flag = 1;
                for(j = next.x-1; j>=0; j--)
                {
                    if(s[j][next.y].t && s[j][next.y].c == 'S')
                    {
                        dis = next.x-j;
                        if(dis%s[j][next.y].v) break;
                        tem = next.step-dis/s[j][next.y].v;
                        if(tem<0) break;
                        if(tem%s[j][next.y].t==0)
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(s[j][next.y].t)
                        break;
                }
                if(!flag)
                    continue;
                for(j = next.x+1; j<=n; j++)
                {
                    if(s[j][next.y].t && s[j][next.y].c == 'N')
                    {
                        dis = j-next.x;
                        if(dis%s[j][next.y].v) break;
                        tem = next.step-dis/s[j][next.y].v;
                        if(tem<0) break;
                        if(tem%s[j][next.y].t==0)
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(s[j][next.y].t)
                        break;
                }
                if(!flag)
                    continue;
                for(j = next.y-1; j>=0; j--)
                {
                    if(s[next.x][j].t && s[next.x][j].c == 'E')
                    {
                        dis = next.y-j;
                        if(dis%s[next.x][j].v) break;
                        tem = next.step-dis/s[next.x][j].v;
                        if(tem<0) break;
                        if(tem%s[next.x][j].t==0)
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(s[next.x][j].t)
                        break;
                }
                if(!flag)
                    continue;
                for(j = next.y+1; j<=m; j++)
                {
                    if(s[next.x][j].t && s[next.x][j].c == 'W')
                    {
                        dis = j-next.y;
                        if(dis%s[next.x][j].v) break;
                        tem = next.step-dis/s[next.x][j].v;
                        if(tem<0) break;
                        if(tem%s[next.x][j].t==0)
                        {
                            flag = 0;
                            break;
                        }
                    }
                    if(s[next.x][j].t)
                        break;
                }
                if(!flag)
                    continue;
                book[next.x][next.y][next.step]=1;
                Q.push(next);
            }
        }
    }
    printf("Bad luck!\n");
}
int main()
{
    int i,j,x,y,t,v;
    while(~scanf("%d%d%d%d",&n,&m,&k,&l))
    {
        char str[3];
        memset(s,0,sizeof(s));
        memset(book,0,sizeof(book));
        for(i = 0; i<k; i++)
        {
            scanf("%s%d%d%d%d",str,&t,&v,&x,&y);
            s[x][y].v = v;
            s[x][y].t = t;
            s[x][y].c = str[0];
        }
        bfs();
    }
    return 0;
}