第三题 0.9 代码 import heapq T=int(input().strip()) for _ in range(T): n=int(input().strip()) x,y=0,0 graph={(0,0)} for i in range(n): a,b=map(int,input().strip().split()) if b==1: dx=dy=0 if a==0: dx=-1 elif a==1: dx=1 elif a==2: dy=-1 else:dy=1 x,y=x+dx,y+dy graph.add((x,y)) if i==n-1: end=(x,y) stack=[(0,(0,0))] used={(0,0)} while stack: dis,point=heapq.heappop(stack) if point==end: print(dis) break for dx,dy in ((-1,0),(1,0),(0,-1),(0,1)): temp=(point[0]+dx,point[1]+dy) if temp not in used and temp in graph: used.add(temp) heapq.heappush(stack,(dis+1,temp))

相关推荐

02-08 15:53
门头沟学院 Java
CoderEcho:让公司知道便宜没好货
点赞 评论 收藏
分享
牛客网
牛客企业服务