|
发表于 2017-6-16 11:24:08
|
显示全部楼层
- import copy
- ##start = [[0,1,2,2], [1,1,2,2], [1,1,2,2], [1,1,2,2]]
- ##end = [[0,2,1,2], [2,1,2,1], [1,2,1,2], [2,1,2,1]]
- start = ['0122112211221122', 0, 0]
- end = ['0212212112122121', 0, 0]
- moves = [[1,0], [-1,0], [0,1], [0, -1]]
- action = {}
- queue = [start]
- index = 0
- flag = True
- while flag:
- for each in moves:
- q = queue[index]
- qx = copy.deepcopy(q)
- if qx == end:
- flag = False
- break
- x = qx[1] + each[0]
- y = qx[2] + each[1]
- if -1 < x < 4 and -1 < y < 4:
- qy = list(qx[0])
- l = 4*x + y
- m = 4*qx[1] + qx[2]
- tmp = qy[l]
- qy[l] = qy[m]
- qy[m] = tmp
- qx[1] = x
- qx[2] = y
- qx[0] = ''.join(qy)
- if qx not in queue:
- queue.append(qx)
- ## action[(qx[1], qx[2])] = (q[1], q[2])
- action[str(qx)] = q
- index += 1
- while qx != start:
- qx = action[str(qx)]
- print(qx)
复制代码
修修补补总算把答案整出来了,但是真的丑~ |
|