Advertisement
Guest User

swaptiles

a guest
May 5th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.20 KB | None | 0 0
  1. import collections
  2.  
  3. def compress(x):
  4.   n = 0
  5.   for i in range(4):
  6.     for j in range(3):
  7.       n = 18*n+x[i][j]
  8.   return n
  9.  
  10. q = collections.deque()
  11. a = [[1, 2, 11+3],[11+2, 11+4, 4], [5, 11+6, 6], [3, 11+5, 11+1]]
  12. s = [a, 0, ""]
  13. q.append(s)
  14. seenset = {compress(a)}
  15. x = 0
  16.  
  17.  
  18. while len(q) > 0:
  19.   st = q.popleft()
  20.   if st[1] > x:
  21.       print(len(q), x)
  22.       x = st[1]
  23.   if st[0][0][2] == 3 and st[0][3][0] == 14:
  24.     print(st[1], st[2])
  25.     break
  26.   for i in range(4):
  27.     for j in range(2):
  28.       d = abs(st[0][i][j]-st[0][i][j+1])
  29.       if d == 11 or d < 6:
  30.         nst = [[[e for e in f] for f in st[0]],st[1]+1,st[2]+str(i)+str(j)+"E,"]
  31.         nst[0][i][j],nst[0][i][j+1]=nst[0][i][j+1],nst[0][i][j]
  32.         if compress(nst[0]) not in seenset:
  33.           seenset.update({compress(nst[0])})
  34.           q.append(nst)
  35.   for i in range(3):
  36.     for j in range(3):
  37.       d = abs(st[0][i][j]-st[0][i+1][j])
  38.       if d == 11 or d < 6:
  39.         nst = [[[e for e in f] for f in st[0]],st[1]+1,st[2]+str(i)+str(j)+"S,"]
  40.         nst[0][i][j],nst[0][i+1][j]=nst[0][i+1][j],nst[0][i][j]
  41.         if compress(nst[0]) not in seenset:
  42.           seenset.update({compress(nst[0])})
  43.           q.append(nst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement