Advertisement
serega1112

Chisla

Mar 26th, 2021
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. from collections import deque
  2.  
  3. start = int(input())
  4. end = int(input())
  5.  
  6. visited = dict()
  7. q = deque([start])
  8. visited[start] = -1
  9.  
  10. def neighbors(num):
  11.     res = []
  12.     if num // 1000 < 9:
  13.         res.append(num + 1000)
  14.     if num % 10 > 1:
  15.         res.append(num - 1)
  16.     res.append(num // 10 + 1000 * (num % 10))
  17.     res.append(num // 1000 + 10 * (num % 1000))
  18.     return res
  19.  
  20. while q:
  21.     cur = q.popleft()
  22.     if cur == end:
  23.         break
  24.     for num in neighbors(cur):
  25.         if num not in visited:
  26.             q.append(num)
  27.             visited[num] = cur
  28.  
  29. res = []
  30. while cur != -1:
  31.     res.append(cur)
  32.     cur = visited[cur]
  33.  
  34. for num in res[::-1]:
  35.     print(num)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement