Escape HDU - 3533 (BFS)

Escape

HDU - 3533

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

题意:
一个人从(0,0)跑到(n,m),只有k点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y
问这个人能不能安全到达终点
要求: 
1.人不能到达炮塔所在的坐标
2.炮塔会挡住子弹
3.途中遇到子弹是安全的,但是人如果停在这个坐标,而子弹也刚好到这个坐标,人就被射死
4.人可以选择停止不动
思路:其实不难,我们只需要看当人位于某个点的时候,其四个方向是否有炮塔,这个炮塔是都向人的方向射击,然后再看子弹是否刚好位于这个坐标即可。
而标记的话,vis[x][y][time],对于time时刻,人位于x,y的情况只需要访问一次,这是唯一的

注意:vis数组因为是三维的并且比较大,如果用int型就会爆内存,用bool就可以节省很多内存就能过了

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
int n,m,k,life;
int to[5][2] = {0,1,1,0,0,-1,-1,0,0,0};//四个方向和停止不动
int mp[105][105];
bool vis[105][105][1005];
struct castle{
    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,nexta;
    queue<node> q;
    int i,j,flag,dis,tem;
    a.x = a.y = a.step = 0;
    q.push(a);
    vis[0][0][0] = 1;
    while(!q.empty()){
        a = q.front();
        q.pop();
        if(a.step > life)
            break;
        if(a.x == n && a.y == m){
            printf("%d\n",a.step);
            return;
        }
        for(int i = 0; i < 5; i++){
            nexta = a;
            nexta.x += to[i][0];
            nexta.y += to[i][1];
            nexta.step++;
            if(check(nexta.x,nexta.y)) continue;
            if(!s[nexta.x][nexta.y].t && !vis[nexta.x][nexta.y][nexta.step] && nexta.step <= life){
                //如果当前点不是碉堡,这个点当前时间没有访问过,还有命可以用
                flag = 1;
                for(j = nexta.x-1; j >= 0; j--){
                    //当位于该点向北找是否有向南射击的碉堡
                    if(s[j][nexta.y].t && s[j][nexta.y].c == 'S'){
                        //找到一个碉堡且往南设计
                        dis = nexta.x - j;//计算炮塔和人的距离
                        if(dis % s[j][nexta.y].v) break;
                        //如果距离不整除速度,说明不可能恰好停在这个点
                        tem = nexta.step - (dis / s[j][nexta.y].v);
                        //人走的时间减去第一个子弹飞行到达这个位置的所需时间
                        if(tem < 0) break;//负数说明第一个子弹都还没有到达这个点
                        if(tem % s[j][nexta.y].t == 0){
                            //如果整除说明后面的子弹会恰好到达,恰好击中
                            flag = 0;
                            break;
                        }
                    }
                    if(s[j][nexta.y].t)//找到碉堡但是不是向南射击,所以一定安全,因为子弹都可以被它挡住
                        break;
                }
                if(!flag)//这个方向死了就死了
                    continue;
                //其他方向同理不在赘述
                for(j = nexta.x+1; j <= n; j++){
                    if(s[j][nexta.y].t && s[j][nexta.y].c == 'N'){
                        dis = j - nexta.x;
                        if(dis % s[j][nexta.y].v) break;
                        tem = nexta.step - dis / s[j][nexta.y].v;
                        if(tem < 0) break;
                        if(tem % s[j][nexta.y].t == 0){
                            flag = 0;
                            break;
                        }
                    }
                    if(s[j][nexta.y].t) break;
                }
                if(!flag) continue;
                for(j = nexta.y-1; j >= 0; j--){
                    if(s[nexta.x][j].t && s[nexta.x][j].c == 'E'){
                        dis = nexta.y - j;
                        if(dis % s[nexta.x][j].v) break;
                        tem = nexta.step - dis / s[nexta.x][j].v;
                        if(tem < 0) break;
                        if(tem % s[nexta.x][j].t == 0){
                            flag = 0;
                            break;
                        }
                    }
                    if(s[nexta.x][j].t) break;
                }
                if(!flag) continue;
                for(j = nexta.y+1; j <= m; j++){
                    if(s[nexta.x][j].t && s[nexta.x][j].c == 'W'){
                        dis = j - nexta.y;
                        if(dis % s[nexta.x][j].v) break;
                        tem = nexta.step - dis / s[nexta.x][j].v;
                        if(tem < 0) break;
                        if(tem % s[nexta.x][j].t == 0){
                            flag = 0;
                            break;
                        }
                    }
                    if(s[nexta.x][j].t) break;
                }
                if(!flag) continue;
                vis[nexta.x][nexta.y][nexta.step] = 1;
                q.push(nexta);
            }
        }
    }
    printf("Bad luck!\n");
}
int main(){
    int i,j,x,y,t,v;
    char str[3];
    while(~scanf("%d%d%d%d",&n,&m,&k,&life)){
        memset(s,0,sizeof(s));
        memset(vis,0,sizeof(vis));
        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;
}

阅读更多

没有更多推荐了,返回首页