UVA - 12294 RPG battles (dp)

本文介绍了一种在典型角色扮演游戏(RPG)中优化战斗策略的方法。玩家需要通过合理使用两种药水来提升自身实力,以尽可能快地赢得所有战斗。文章详细解释了如何通过动态规划算法来解决这一问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In many typical RPG games, you battle with bad guys, creatures, monsters or ghosts etc. all the time.
After each battle, you may get magic potions that power you up, so you’ll get stronger and stronger,
and finally defeat the big boss. In this problem, we only consider two kinds of potion: stronger and
double power. If you drink a bottle of stronger potion, your power increases by 1; if you drink a bottle
of double power potion, your power doubles.
How long each battle lasts depends on how powerful you are. Each battle is described by six
parameters: p1, p2, t1, t2, w1, w2. That means, if your power is less than p1, you will be defeated; if
your power is greater than p2, you’ll need t2 seconds to defeat all the enemies. If your power is between
p1 and p2 (inclusive), the time needed for the battle decreases linearly from t1 to t2. For example, if
p1 = 50, p2 = 75, t1 = 40, t2 = 15, and your power is 55, then the battle lasts for 35 seconds. Note that
the time needed for battles may be non-integers. The last two parameters, w1 and w2, represent the
number of bottles of stronger potion and double power potion you got after winning the battle. Note
that you don’t have to drink these potions immediately. You can drink them later if that’ll decrease
the total time. You cannot drink potions during a battle, so the time needed for battles is not affected
by the potions.
Given the list of battles, your task is to find a strategy to win all the battles as quickly as possible.
Note that you must enter the battles in order. You cannot redo or skip any battle.
Input
There will be at most 25 test cases. Each test begins with two integers n and p (1 ≤ n ≤ 1000,
1 ≤ p ≤ 100), the number of battles and your initial power. Each of the next n lines contains 6 integers
p1, p2, t1, t2, w1, w2 (1 ≤ p1 < p2 ≤ 100, 1 ≤ t2 < t1 ≤ 100, 0 ≤ w1, w2 ≤ 10). The input is terminated
by a test case with n = p = 0, you should not process it.
Output
For each test case, print the shortest time (in seconds) to win all the battles, to two digits after the
decimal point. If you cannot win all the battles, print ‘Impossible’ (without quotes).
Sample Input
1 55
50 75 40 15 10 0
2 55
50 75 40 15 10 0
50 75 40 15 10 0
3 1
1 2 2 1 0 5
1 2 2 1 1 0
1 100 100 1 0 0
1 7
4 15 35 23 0 0
1 1
2 3 2 1 0 0
0 0
Sample Output
35.00
60.00
41.00
31.73

Impossible

题意 :给你两个值 n,p 一个是战斗次数,一个是初始力量值 (我们必须按照顺序完成战斗),我们有两种药水 ,1 . 力量+1   2 . 力量*2 , 我们完成战斗花费时间  ,要是力量在p1下,不能完成战斗 ,大于或等于p2 ,时间为t2 ,在p1 -p2 之间 ,力量和时间成线性关系  如 力量55  p1 50  p2  75  t1  40  t2  15 ,时间为35  

显然我们有+1就把+1喝掉这是最优的 ,然后我们可以选择是否喝*2 ,因为 早喝和晚喝增长力量可能不同 

那么  dp[i][j][k]  i代表第几场比赛结束 ,j 代表当前力量 ,k 代表剩余的*2药水数  ,dp存的值为最小时间 

比如 dp[1][10][1]=1.0 代表第1场比赛结束后,你有10点力量,1瓶药水 ,到这种情况的最小时间为1.0

因为 p1,p2范围最大为100 ,所以dp范围到100就好 ,有再多力量也只需要100 ,那么 药水最多7瓶肯定力量会超过100

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<math.h>
#include<queue>
#include<map>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
int n,p;
double p1,p2,t1,t2;
double dp[1100][200][10];
int w1[1100];
int w2[1100];
bool v[1110][200][10];//这种情况是否出现过
double pp(double x,double y)//输出花费时间
{
    if(x>=p2) return t2;
    return t1-(x-p1)*y;
}
int main()
{
  while(~scanf("%d%d",&n,&p)&&n+p)
  {
      memset(v,0,sizeof(v));
      memset(dp,0,sizeof(dp));
      v[0][p][0]=1;
        for(int i=1;i<=n;i++)
        {
        scanf("%lf%lf%lf%lf%d%d",&p1,&p2,&t1,&t2,&w1[i],&w2[i]);
        double pos=(t1-t2)/(p2-p1);
        for(int j=p1;j<=100;j++)//最大力量100
        {
            for(int k=0;k<=7;k++)//最多有7瓶药水
                if(v[i-1][j][k]) 
                {
                    int e=min(7,k+w2[i-1]);//我们最多只需要7瓶药
                    int e1=min(j+w1[i-1],100);//最大力量只需要100
                    for(int kk=e;kk>=0;kk--,e1*=2)
                    {
                        if(e1>=100) //超过100按100计算,而且直接break ,因为你不需要在嗑药了,力量达上限
                        {
                            if(v[i][100][kk])
                            {
                                dp[i][100][kk]=min(dp[i][100][kk],dp[i-1][j][k]+pp(100.0,pos));
                            }
                            else
                            {
                                v[i][100][kk]=1;
                                dp[i][100][kk]=dp[i-1][j][k]+pp(100.0,pos);
                            }
                            break;

                        }
                        if(!v[i][e1][kk])
                        {
                            dp[i][e1][kk]=dp[i-1][j][k]+pp(e1*1.0,pos);
                            v[i][e1][kk]=1;
                        }
                        else
                            dp[i][e1][kk]=min(dp[i][e1][kk],dp[i-1][j][k]+pp(e1*1.0,pos));
                    }
                }
        }

        }
        double ans=inf;
        for(int i=1;i<=100;i++)
            for(int j=0;j<10;j++)
              if(v[n][i][j])
              ans=min(ans,dp[n][i][j]);
        if(ans==inf)
            printf("Impossible\n");
        else
            printf("%.2lf\n",ans);
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值