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
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从(0,0)出发要在d秒内到达(n,m),有以下的限制条件
1)有k个炮台,不能走,且其会每t[i]秒发射一颗v[i] m/s的炮弹
2)小A不能和炮弹在恰好整数秒相遇,否则会阵亡
3)炮弹会一直移动,直到遇到炮台或出界
4)小A可以原地停留,同时也会消耗时间

思路
预处理出1~d秒有炮弹的位置shoot[x][y][t]=1(则在第t秒的(x,y)有炮弹。用visit[x][y][t]代表在第t秒访问的(x,y)的状态,然后常规bfs即可

#include <iostream>
#include <queue>
#include <cstring>
#include <vector>
using namespace std;

int l,r,d,m;
char c[105];
int x[105],y[105],v[105],t[105];
char s[105][105];
int X[5]= {1,-1,0,0,0};
int Y[5]= {0,0,1,-1,0};
bool visit[105][105][1005];
bool shoot[101][101][1005];

struct node
{
    int x,y,step;
    node():step(0){}
    node(int a,int b,int d):x(a),y(b),step(d){}
} S,e,temp;

void getpos(char tar,int &nx,int &ny)
{
    if(tar=='S') nx=X[0],ny=Y[0];
    if(tar=='N') nx=X[1],ny=Y[1];
    if(tar=='E') nx=X[2],ny=Y[2];
    if(tar=='W') nx=X[3],ny=Y[3];
}

void getshoot()///预处理
{
    int nx,ny;
    for(int i=1; i<=m; i++)
        cin>>c[i]>>t[i]>>v[i]>>x[i]>>y[i],s[x[i]][y[i]]='#';
    memset(shoot,0,sizeof(shoot));
    for(int i=1; i<=m; i++)  ///枚举堡垒
    {
        getpos(c[i],nx,ny);  ///nx,ny是射击方向
        int sx=x[i],sy=y[i]; ///堡垒位置
        for(int k=0; k<=d; k+=t[i]) ///模拟第k个子弹发射的时间
        {
            for(int j=1;;j++) ///j为移动的距离
            {
                int px=sx+nx*j,py=sy+ny*j;
                if(px<0||px>l||py>r||py<0||s[px][py]=='#')///碰到堡垒就停下
                    break;
                if(j%v[i]==0)///当前位置子弹可以到达,到达时间为j/v[i]
                    shoot[px][py][k+j/v[i]]=true;
            }
        }
    }
}

bool check(int x,int y,int step)
{
    if(s[x][y]=='#'||x<0||x>l)  return false;
    if(y>r||y<0||visit[x][y][step]) return false;
    if(shoot[x][y][step])  return false;
    return true;
}

int solve()
{
    visit[S.x][S.y][S.step]=true;
    queue<node> q;
    q.push(S);
    while(!q.empty())
    {
        temp=q.front();
        q.pop();
        if(temp.step>d)  continue;///没能量了
        if(temp.x==e.x&&temp.y==e.y)  return temp.step;
        for(int i=0; i<5; i++)
        {
            int  nx=temp.x+X[i] , ny=temp.y+Y[i];
            if(!check(nx,ny,temp.step+1)) continue;
            visit[nx][ny][temp.step+1]=true;
            q.push({nx,ny,temp.step+1});
        }
    }
    return -1;
}

int main()
{
    while(cin>>l>>r>>m>>d)
    {
        memset(visit,0,sizeof(visit));
        for(int i=0; i<=l; i++)
            for(int j=0; j<=r; j++)
                s[i][j]='.';
        getshoot();///预处理出每一秒有炮弹的位置
        S.x=0,S.y=0;///从(0,0)出发
        e.x=l,e.y=r;
        int ans=solve();
        if(ans==-1) cout<<"Bad luck!"<<endl;///-1忘了判
        else  cout<<ans<<endl;
    }
    return 0;
}

### 关于HDU - 6609 的题目解析 由于当前未提供具体关于 HDU - 6609 题目的详细描述,以下是基于一般算法竞赛题型可能涉及的内容进行推测和解答。 #### 可能的题目背景 假设该题目属于动态规划类问题(类似于多重背包问题),其核心在于优化资源分配或路径选择。此类问题通常会给出一组物品及其属性(如重量、价值等)以及约束条件(如容量限制)。目标是最优地选取某些物品使得满足特定的目标函数[^2]。 #### 动态转移方程设计 如果此题确实是一个变种的背包问题,则可以采用如下状态定义方法: 设 `dp[i][j]` 表示前 i 种物品,在某种条件下达到 j 值时的最大收益或者最小代价。对于每一种新加入考虑范围内的物体 k ,更新规则可能是这样的形式: ```python for i in range(n): for s in range(V, w[k]-1, -1): dp[s] = max(dp[s], dp[s-w[k]] + v[k]) ``` 这里需要注意边界情况处理以及初始化设置合理值来保证计算准确性。 另外还有一种可能性就是它涉及到组合数学方面知识或者是图论最短路等相关知识点。如果是后者的话那么就需要构建相应的邻接表表示图形结构并通过Dijkstra/Bellman-Ford/Floyd-Warshall等经典算法求解两点间距离等问题了[^4]。 最后按照输出格式要求打印结果字符串"Case #X: Y"[^3]。 #### 示例代码片段 下面展示了一个简单的伪代码框架用于解决上述提到类型的DP问题: ```python def solve(): t=int(input()) res=[] cas=1 while(t>0): n,k=list(map(int,input().split())) # Initialize your data structures here ans=find_min_unhappiness() # Implement function find_min_unhappiness() res.append(f'Case #{cas}: {round(ans)}') cas+=1 t-=1 print("\n".join(res)) solve() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值