Advertisement
webalorn

Cat problem

Apr 28th, 2024
972
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.67 KB | None | 0 0
  1. import sys
  2. from collections import deque
  3. def cat_jump(s):
  4.     return tuple([(k > 0 and s[k-1]) or (k<n-1 and s[k+1]) for k in range(n)])
  5.  
  6. n = 10
  7. START = (True,)*n
  8. END = (False,)*n
  9. seen = {START: None} # sit : (prev, action)
  10. sits = deque([(START, None)]) # (sit, prev)
  11.  
  12. while sits and END not in seen:
  13.     s, prev = sits.popleft()
  14.     for k in range(n):
  15.         s2 = list(s)
  16.         s2[k] = False
  17.         s2 = cat_jump(s2)
  18.         if not s2 in seen:
  19.             seen[s2] = (s, k)
  20.             sits.append((s2, s))
  21.  
  22. s = END
  23. actions = []
  24. while seen[s] is not None:
  25.     s, a = seen[s]
  26.     actions.append(a+1)
  27. print(f'{len(actions)} actions')
  28. print(*actions[::-1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement