HDU 4310题解

本文介绍了一种在《Dota》游戏中,面对1vN困境时如何选择最佳策略,以最小化自身生命值损失,杀死所有敌人的方法。关键在于优先集火攻击力最高且生命值最低的敌人。
摘要由CSDN通过智能技术生成

Problem:

When playing DotA with god-like rivals and pig-like team members, you have to face an embarrassing situation: All your teammates are killed, and you have to fight 1vN。

There are two key attributes for the heroes in the game, health point (HP) and damage per shot (DPS). Your hero has almost infinite HP, but only 1 DPS。

To simplify the problem, we assume the game is turn-based, but not real-time. In each round, you can choose one enemy hero to attack, and his HP will decrease by 1. While at the same time, all the lived enemy heroes will attack you, and your HP will decrease by the sum of their DPS. If one hero's HP fall equal to (or below) zero, he will die after this round, and cannot attack you in the following rounds。

Although your hero is undefeated, you want to choose best strategy to kill all the enemy heroes with minimum HP loss。

Input:

The first line of each test case contains the number of enemy heroes N (1 <= N <= 20). Then N lines followed, each contains two integers DPSi and HPi, which are the DPS and HP for each hero. (1 <= DPSi, HPi <= 1000)。

Output:

Output one line for each test, indicates the minimum HP loss。

Sample Input:

请输入敌人的数量:4
请输入这个敌人的生命值和攻击力:2 10
请输入这个敌人的生命值和攻击力:1 10
请输入这个敌人的生命值和攻击力:2 3
请输入这个敌人的生命值和攻击力:2 2

Sample Output:

损失的最小生命值为:44

 思路:

每回合可以选择一名敌人进行攻击,他的生命值减少1,每个敌人都有hp和attack,要求选择最佳策略以最小的 HP 损失杀死所有敌人。要受到伤害最小,也就是先判断每个敌人的攻击力如何,攻击力越高的敌人,就是我们先要集火的目标。而且在相同攻击力下我们需要攻击生命值最小的敌人。在我的代码中,首先输入用例,我用了一个结构体Attribute来定义敌人,每个结构体的属性有hp,attack这两个,然后通过sort进行排序,按照攻击力从大到小排序的同时,将攻击力相同的敌人生命值按从小到大排序,最先集火,再用min记录失去的血量,此时min为最小失去的生命值。

代码:

#include <iostream> 
#include <vector>   
#include <algorithm> 

using namespace std;
const int maxn = 20;

// 定义敌人属性,包含生命值和攻击力
struct Attribute {
    int hp; // 生命值
    double attack; // 攻击力
}Hero[maxn];

// 按照攻击力从大到小排序的同时,将攻击力相同的敌人生命值按从小到大排序
bool compare(const Attribute& a, const Attribute& b) {
    if (a.attack == b.attack) {
        return a.hp < b.hp; // 如果攻击力相同,则按照生命值从小到大排序
    }
    return a.attack > b.attack; // 否则按照攻击力从大到小排序
}

int main() {
    int n;
    cout << "请输入敌人的数量:";
    cin >> n;
    int min = 0; // 初始化损失的最小生命值为0

    for (int i = 0; i < n; i++) {
        cout << "请输入这个敌人的生命值和攻击力:";
        cin >> Hero[i].hp >> Hero[i].attack; // 输入每个敌人的属性
    }
    sort(Hero, Hero + n, compare); // 对敌人按照攻击力进行排序

    int i = 0; // 如果当前被攻击的敌人生命值还没归零的话,无法结束当前循环进入下一循环
    for (; i < n; ) {
        // 每次遍历一个敌人时,假设其受到攻击一次,生命值减1
        int x = i; // 记录当前敌人的索引
        Hero[i].hp--; // 减去一次生命值
        if (Hero[i].hp == 0) { // 如果被攻击的敌人生命值为0,则没有受到这个敌人的攻击
            x++;
            i++;
        }
        for (int m = x; m < n; m++) {
            min += Hero[m].attack; // 计算当前敌人后续攻击造成的伤害
        }
    }
    
    cout << "损失的最小生命值为:" << min << endl;

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值