第三题自己怎么写测试样例都是对的,就是0,要气哭了,花了2个小时也没看出来为啥子。
def solution(moveInfo):
distances = {(0, 0): 0}
x, y = 0, 0
directions = [(0, 1), (0, -1), (-1, 0), (1, 0)]
minDistance = 0
for d, flag in moveInfo:
if flag == 1:
x, y = x + directions[d][0], y + directions[d][1]
minDistance += 1
if distances.get((x, y), float("-inf")) == float("-inf"):
# 判断邻接点是否访问过
for dx, dy in [(0, 1), (0, -1), (-1, 0), (1, 0)]:
temp = distances.get((x + dx, y + dy), float("inf")) + 1
if temp < minDistance:
minDistance = temp
distances.update({(x, y): minDistance})
else:
if minDistance < distances[(x, y)]:
distances[(x, y)] = minDistance
else:
minDistance = distances[(x, y)]
return minDistance
if __name__ == "__main__":
T = int(input())
ans = []
for _ in range(T):
moveInfo = []
N = int(input())
for n in range(N):
moveInfo.append([int(v) for v in input().split()])
ans.append(solution(moveInfo))
print("\n".join([str(v) for v in ans]))
全部评论
(10) 回帖