今天 的每日两题就总结下午打的一个比赛的两个题
Shopee Xpress Delivery
迷宫寻找最短路问题, 而且在地图添加了无消耗转移点
使用BFS解决, 首先构建 转移点列表, 从 0,0 点开始 ,可移动方向是上,下,左,右 和同类型的转移点的位置,
对于可移动的点,判断是否被访问过,将其添加到队列中,若 终点被添加 那么返回路径值
import collections
m,n = [int(num) for num in input().strip().split()]
gird =[]
dic = [[]for _ in range(260)]
for i in range(m):
nums= []
for num in input().strip().split():
num = int(num)
nums.append(num)
if num!= 0:
dic[num].append((i,len(nums)-1))
gird.append(nums)
vist = [[0]*n for _ in range(m)]
vist[0][0]=1
# print(gird)
# print(dic)
que=collections.deque([[0,0,0]])
while que:
x,y,w = que.popleft()
if x==m-1 and y ==n-1:
print(w)
break
for nx,ny in [[x+1,y],[x-1,y],[x,y+1],[x,y-1]]:
if 0<=nx<m and 0<=ny<n and vist[nx][ny]==0:
que.append([nx,ny,w+1])
vist[nx][ny]=1
for nx,ny in dic[gird[x][y]] :
if vist[nx][ny]==0:
que.append([nx,ny,w])
vist[nx][ny]=1
Installation of a Shopee Billboard
参考416. 分割等和子集 使用动态规划来判断一个集合是否可以被二分, 然后使用组合数来取原列表的组合,然后判断该子集能否被二分
from itertools import combinations
n = int(input())
nums= [int(num) for num in input().split()]
if n < 2:
print(0)
flag = 0
res = 0
for i in range(n,1,-1):
liss = list(combinations(range(0,n), i))
for lis in liss:
tmp =[]
for i in lis :
tmp.append(nums[i])
total = sum(tmp)
if total % 2 != 0:
continue
target = total // 2
if target >=res:
dp = [True] + [False] * target
for i, num in enumerate(tmp):
for j in range(target, num - 1, -1):
dp[j] |= dp[j - num]
if dp[target]:
res = max(res,target)
flag =1
# break
print(res)