Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import collections
- def compress(x):
- n = 0
- for i in range(4):
- for j in range(3):
- n = 18*n+x[i][j]
- return n
- q = collections.deque()
- a = [[1, 2, 11+3],[11+2, 11+4, 4], [5, 11+6, 6], [3, 11+5, 11+1]]
- s = [a, 0, ""]
- q.append(s)
- seenset = {compress(a)}
- x = 0
- while len(q) > 0:
- st = q.popleft()
- if st[1] > x:
- print(len(q), x)
- x = st[1]
- if st[0][0][2] == 3 and st[0][3][0] == 14:
- print(st[1], st[2])
- break
- for i in range(4):
- for j in range(2):
- d = abs(st[0][i][j]-st[0][i][j+1])
- if d == 11 or d < 6:
- nst = [[[e for e in f] for f in st[0]],st[1]+1,st[2]+str(i)+str(j)+"E,"]
- nst[0][i][j],nst[0][i][j+1]=nst[0][i][j+1],nst[0][i][j]
- if compress(nst[0]) not in seenset:
- seenset.update({compress(nst[0])})
- q.append(nst)
- for i in range(3):
- for j in range(3):
- d = abs(st[0][i][j]-st[0][i+1][j])
- if d == 11 or d < 6:
- nst = [[[e for e in f] for f in st[0]],st[1]+1,st[2]+str(i)+str(j)+"S,"]
- nst[0][i][j],nst[0][i+1][j]=nst[0][i+1][j],nst[0][i][j]
- if compress(nst[0]) not in seenset:
- seenset.update({compress(nst[0])})
- q.append(nst)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement