度小满4.20笔试第二题城市旅行最小价值BFS
考试的时候忘了一个细节导致一个用例都没过,最后没时间了,考完试才发现忘了题目的下标值不是从0开始的,修改完是可以通过用例的,不知道这样做思路是否正确,有没有大佬指导下?
import sys from collections import deque def parse_nums(nums_str): return [int(x) for x in nums_str.strip().split()] def bfs(cs, a, b, c): target = len(cs) - 1 visited = [False] * len(cs) queue = deque() queue.append((0, 0)) visited[0] = True direcitions = [(0, a), (1, a + c), (-1, a + b)] while queue: x, t = queue.popleft() for d in direcitions: nx, nt = cs[x] + d[0], t + d[1] if 0 <= nx < len(cs) and not visited[nx]: if nx == target: return nt queue.append((nx, nt)) visited[nx] = True return -1 for line in sys.stdin: n, a, b, c = parse_nums(line) cs = parse_nums(input()) # 忘了写这两行啊啊啊啊 for i in range(len(cs)): cs[i] -= 1 print(bfs(cs, a, b, c)) # 答案4#度小满笔试##度小满##笔试题目#