fork download
  1. from collections import deque
  2. #作者堀江、特に賢い解法も思いつかないので平凡なコード
  3. #会津大学オンラインジャッジ問0121Seven Puzzle書きかけ.合格。
  4.  
  5. def f():
  6. hs={}
  7. ds=[[1,4],[2,5,0],[3,6,1],[7,2],[5,0],[6,1,4],[7,2,5],[3,6]]
  8. e=(7,1234567)
  9. qs=deque([e])
  10. hs[e[1]]=0
  11. while len(qs)>0:
  12. e=qs.popleft()
  13. i,n=e[0],e[1]
  14. l=hs[n]+1
  15. r1=10**i
  16. for j in ds[i]:
  17. r2=10**j
  18. b2=(n//r2)%10
  19. n2=n+b2*(r1-r2)
  20. if (n2 in hs)==False:
  21. hs[n2]=l
  22. qs.append((j,n2))
  23. return hs
  24. try:
  25. hs=f()
  26. while True:
  27. n=int("".join(input().split(" ")))
  28. print(hs[n])
  29. except EOFError:
  30. next
Success #stdin #stdout 0.08s 10848KB
stdin
0 1 2 3 4 5 6 7
1 0 2 3 4 5 6 7
7 6 5 4 3 2 1 0
stdout
0
1
28