当前位置: 面试刷题>> 预测能否胜利 (经典算法题500道)


题目描述补充

题目:预测能否胜利(战争模拟)

在一场模拟的战争中,你有两支部队,分别称为A部队和B部队。每支部队由多个单位组成,每个单位有其特定的攻击力(attack power)和生命值(health points, HP)。战斗采用回合制,每个回合,双方各自选择一个单位进行攻击,被攻击的单位生命值减少攻击单位的攻击力数值。如果某个单位的生命值降至0或以下,则该单位被视为“阵亡”。

战斗的目标是彻底消灭对方所有单位。给定两支部队的单位列表(每个单位包含攻击力和生命值),预测并返回哪支部队能最终取得胜利,或者如果战斗会进入无限循环(即双方都没有能力消灭对方最后一个单位),则返回“平局”。

示例

输入

  • A部队单位列表:[[5, 10], [3, 5]](表示A部队有两个单位,第一个单位攻击力5,生命值10;第二个单位攻击力3,生命值5)
  • B部队单位列表:[[4, 8]](表示B部队有一个单位,攻击力4,生命值8)

输出"A"(A部队能最终取得胜利)

PHP 示例代码

function predictVictory($unitsA, $unitsB) {
    while (!empty($unitsA) && !empty($unitsB)) {
        // 选择攻击力最高的单位进行攻击(简化策略)
        usort($unitsA, function($a, $b) { return $b[0] - $a[0]; });
        usort($unitsB, function($a, $b) { return $b[0] - $a[0]; });

        $attackA = array_shift($unitsA);
        $attackB = array_shift($unitsB);

        if ($attackA[0] >= $attackB[1]) {
            // A部队的单位能够消灭B部队的单位
            continue;
        } else {
            $attackB[1] -= $attackA[0];
            if ($attackB[1] <= 0) {
                // B部队的单位被消灭
                continue;
            } else {
                // B部队的单位存活,重新加入队列
                array_unshift($unitsB, $attackB);
            }
        }

        // 同理处理B部队攻击A部队的情况
        if ($attackB[0] >= $attackA[1] && !empty($unitsA)) {
            $unitsA = []; // A部队单位被消灭
        } elseif (!empty($unitsA)) {
            $unitsA[0][1] -= $attackB[0];
            if ($unitsA[0][1] <= 0) {
                array_shift($unitsA); // 移除已阵亡的单位
            }
        }
    }

    return empty($unitsA) ? 'B' : (empty($unitsB) ? 'A' : '平局');
}

// 示例调用
$unitsA = [[5, 10], [3, 5]];
$unitsB = [[4, 8]];
echo predictVictory($unitsA, $unitsB); // 输出: A

Python 示例代码

def predict_victory(unitsA, unitsB):
    while unitsA and unitsB:
        # 简化处理,选择攻击力最大的单位
        max_attackA = max(unitsA, key=lambda x: x[0])
        max_attackB = max(unitsB, key=lambda x: x[0])

        unitsA.remove(max_attackA)
        unitsB.remove(max_attackB)

        if max_attackA[0] >= max_attackB[1]:
            continue  # A部队消灭B部队单位
        else:
            max_attackB = [max_attackB[0], max_attackB[1] - max_attackA[0]]
            if max_attackB[1] > 0:
                unitsB.append(max_attackB)

        if unitsA and max_attackB[0] >= unitsA[0][1]:
            unitsA = []  # B部队消灭A部队剩余单位
        elif unitsA:
            unitsA[0] = [unitsA[0][0], unitsA[0][1] - max_attackB[0]]
            if unitsA[0][1] <= 0:
                unitsA.pop(0)

    return 'B' if not unitsA else ('A' if not unitsB else '平局')

# 示例调用
unitsA = [[5, 10], [3, 5]]
unitsB = [[4, 8]]
print(predict_victory(unitsA, unitsB))  # 输出: A

JavaScript 示例代码

function predictVictory(unitsA, unitsB) {
    while (unitsA.length > 0 && unitsB.length > 0) {
        // 选择攻击力最大的单位
        const maxA = unitsA.reduce((a, b) => a[0] > b[0] ? a : b);
        const maxB = unitsB.reduce((a, b) => a[0] > b[0] ? a : b);

        unitsA = unitsA.filter(unit => unit !== maxA);
        unitsB = unitsB.filter(unit => unit !== maxB);

        if (maxA[0] >= maxB[1]) {
            continue; // A部队消灭B部队单位
        } else {
            const newMaxB = [maxB[0], maxB[1] - maxA[0]];
            if (newMaxB[1] > 0) {
                unitsB.push(newMaxB);
            }
        }

        if (unitsA.length > 0 && maxB[0] >= unitsA[0][1]) {
            unitsA = []; // B部队消灭A部队剩余单位
        } else if (unitsA.length > 0) {
            unitsA[0] = [unitsA[0][0], unitsA[0][1] - maxB[0]];
            if (unitsA[0][1] <= 0) {
                unitsA.shift();
            }
        }
    }

    return unitsA.length === 0 ? 'B' : (unitsB.length === 0 ? 'A' : '平局');
}

// 示例调用
const unitsA = [[5, 10], [3, 5]];
const unitsB = [[4, 8]];
console.log(predictVictory(unitsA, unitsB)); // 输出: A

码小课网站中有更多相关内容分享给大家学习,希望这些示例和描述能帮助你更好地理解和解决这类问题。

推荐面试题