博主头像
HailinCode

后端工程师

算法

倒水

蓝桥杯-倒水二分答案 n, k = map(int, input().split()) a = list(map(int, input().split())) def check(mid): b = a[:] # 从前往后,贪心地倒水 for i in range(n - k): if b[i] > mid: # 把多余的倒给后面同

算法

九宫重排

蓝桥杯-九宫重排这道题一眼BFS,求最短路径问题。所以写了最优解一坨屎山! import sys from collections import deque # 读取初始状态和目标状态字符串 s = input() target = input() # 初始化起始位置(空格'.'的位置) start_x = 0 start_y = 0 # 创建 3x3 的网格表示 grid = [['']

算法

完全背包问题

完全背包问题 Python描述 """完全背包问题""" from functools import cache # 有n种物品,第i种物品的体积为w[i],价值为v[i],每种物品无限次重复选 # 求体积和不超过 capacity 时的最大价值 """选或者不选""" # 选:剩余容量减少w[i] # 不选:容量不变 """子问题""" # 在剩余容量为 c 时,从前 i 种物品中得到的最

算法

01背包问题

from functools import cache from typing import List """01背包问题""" """求方案数问题 总方案数:两个互斥方案相加""" def zero_one_knapsack(capacity: int, weights: list[int], values: list[int]): n = len(weights) @ca