Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
9-liner: no import squeezed solution in Creative category for 8 Puzzle by przemyslaw.daniel
def checkio(p):
q, v = [("", tuple(map(tuple, p)))], set([])
for s, p in ((a,b) for a, b in q if b not in v):
if p == ((1,2,3), (4,5,6), (7,8,0)): return s
x, y = divmod(sum(p, ()).index(0), 3); v |= {p}
for i, j, d in [(1,0,"D"),(-1,0,"U"),(0,1,"R"),(0,-1,"L")]:
t, g, h = [list(x) for x in p], (x+i)%3, (y+j)%3
t[x][y], t[g][h] = t[g][h], t[x][y]
q += [(s+d, tuple(map(tuple,t)))]*(0<=x+i<3)*(0<=y+j<3)
Jan. 10, 2018
Comments: