笔试-士兵过河

应用

一支N个士兵的军队正在趁夜色逃亡,途中遇到一条湍急的大河。敌军在T的时长后到达河面,没到过对岸的士兵都会被消灭。现在军队只找到了1只小船,这船最多能同时坐上2个士兵。

1.当1个士兵划船过河,用时为 a[i]; 0<=i<N

2.当2个士兵坐船同时划船过河时,用时为 max(a[j],a[i])两士兵中用时最长的

3.当2个士兵坐船 1 个士兵划船时,用时为 a[i]*10; a[i]为划船士兵用时

4.如果士兵下河游泳,则会被湍急水流直接带走,算作死亡。

请帮忙给出一种解决方案,保证存活的士兵最多,且过河用时最短。

实现

python 复制代码
N = int(input("士兵数量:"))
T = int(input("敌军到达时长:"))

strs = input("每个士兵过河时长:").split()
left = [int(i) for i in strs]

right = []
total = 0

result = [0, T]

def GO(left, right, total):

    left.sort()
    
    L = [x for x in left]
    R = [x for x in right]

    if len(left) == 0 or total > T:
        # print(len(right), total)
        # 实时更新存活人数、时长
        if len(right) > result[0]:
            result[0] = len(right)
            result[1] = total
        
        if len(right) == result[0]:
            if total < result[1]:
                result[1] = total

    if len(left) == 1 and total <= T:
        # 第一种方案
        s = left[0]
        t = total + s

        if t <= T:
            right.append(s)
            left.remove(s)

            BACK(left, right, t)
            # 复原
            left = [x for x in L]
            right = [x for x in R]
        else:
            if len(right) > result[0]:
                result[0] = len(right)
                result[1] = total
            
            if len(right) == result[0]:
                if total < result[1]:
                    result[1] = total
 

    if len(left) >= 2 and total <= T:
        # 第二、三种方法
        for i in range(0, len(left)):
            for j in range(i+1, len(left)):

                s1 = left[i]
                s2 = left[j]

                t = total + min(s2, s1 * 10)

                if t <= T:
                    right.append(s1)
                    right.append(s2)
                    left.remove(s1)
                    left.remove(s2)

                    BACK(left, right, t)

                    left = [x for x in L]
                    right = [x for x in R]
                else:
                    if len(right) > result[0]:
                        result[0] = len(right)
                        result[1] = total
                    
                    if len(right) == result[0]:
                        if total < result[1]:
                            result[1] = total

def BACK(left, right, total):
    
    right.sort()

    L = [x for x in left]
    R = [x for x in right]

    if len(left) == 0 or total > T:
        if len(right) > result[0]:
            result[0] = len(right)
            result[1] = total
        
        if len(right) == result[0]:
            if total < result[1]:
                result[1] = total

    if len(left) > 0 and total <= T:
        # 第一种方案
        s = right[0]
        t = total + s

        if t <= T:
            left.append(s)
            right.remove(s)

            GO(left, right, t)
            
            left = [x for x in L]
            right = [x for x in R]
        else:
            if len(right) > result[0]:
                result[0] = len(right)
                result[1] = total
            
            if len(right) == result[0]:
                if total < result[1]:
                    result[1] = total

# 运行
GO(left, right, total)
# print(len(result))
print(result[0], result[1])
powershell 复制代码
士兵数量:5
敌军到达时长:43
每个士兵过河时长:12 13 15 20 50
3 40

士兵数量:5
敌军到达时长:130
每个士兵过河时长:50 12 13 15 20
5 128

士兵数量:7
敌军到达时长:171
每个士兵过河时长:25 12 13 15 20 35 20
7 171
相关推荐
知行合一。。。4 小时前
Python--04--数据容器(总结)
开发语言·python
架构师老Y4 小时前
008、容器化部署:Docker与Python应用打包
python·容器·架构
lifewange5 小时前
pytest-类中测试方法、多文件批量执行
开发语言·python·pytest
pluvium275 小时前
记对 xonsh shell 的使用, 脚本编写, 迁移及调优
linux·python·shell·xonsh
2401_827499995 小时前
python项目实战09-AI智能伴侣(ai_partner_5-6)
开发语言·python
PD我是你的真爱粉5 小时前
MCP 协议详解:从架构、工作流到 Python 技术栈落地
开发语言·python·架构
ZhengEnCi5 小时前
P2G-Python字符串方法完全指南-split、join、strip、replace的Python编程利器
python
是小蟹呀^6 小时前
【总结】LangChain中工具的使用
python·langchain·agent·tool
宝贝儿好6 小时前
【LLM】第二章:文本表示:词袋模型、小案例:基于文本的推荐系统(酒店推荐)
人工智能·python·深度学习·神经网络·自然语言处理·机器人·语音识别
王夏奇6 小时前
pythonUI界面弹窗设置的几种办法
python·ui