# 773. Sliding Puzzle 滑动谜题

## # 题目描述：

On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.

A move consists of choosing 0 and a 4-directionally adjacent number and swapping it.

The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]].

Given a puzzle board, return the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, return -1.

Examples 1:

``````Input: board = [[1,2,3],[4,0,5]]
Output: 1
Explanation: Swap the 0 and the 5 in one move.
``````

Examples 2:

``````Input: board = [[1,2,3],[5,4,0]]
Output: -1
Explanation: No number of moves will make the board solved.
``````

Examples 3:

``````Input: board = [[4,1,2],[5,0,3]]
Output: 5
Explanation: 5 is the smallest number of moves that solves the board.
An example path:
After move 0: [[4,1,2],[5,0,3]]
After move 1: [[4,1,2],[0,5,3]]
After move 2: [[0,1,2],[4,5,3]]
After move 3: [[1,0,2],[4,5,3]]
After move 4: [[1,2,0],[4,5,3]]
After move 5: [[1,2,3],[4,5,0]]
Input: board = [[3,2,4],[1,5,0]]
Output: 14
``````

Note:

1. board will be a 2 x 3 array as described above.
2. board[i][j] will be a permutation of [0, 1, 2, 3, 4, 5].

## # 解题方法

Hard题目真的是一个比一个看起来难，但是只要有充足的经验，能看出这个是考BFS的题目，那么剩下的时间就是套用模板了吧。。

Ps，吐槽一句，python的字符画不支持直接指定某个位置的字符，因此这个题里面迫不得已用了几次string和list互转的过程。。

``````class Solution(object):
def slidingPuzzle(self, board):
"""
:type board: List[List[int]]
:rtype: int
"""
goal = "123450"
start = self.board2str(board)

bfs = collections.deque()
bfs.append((start, 0))
visited = set()

dirs = [(0, 1), (1, 0), (0, -1), (-1, 0)]

while bfs:
path, step = bfs.popleft()

if path == goal:
return step
p = path.index("0")
x, y = p / 3, p % 3
path = list(path)
for dir in dirs:
tx, ty = x + dir[0], y + dir[1]
if tx < 0 or tx >= 2 or ty < 0 or ty >= 3:
continue
path[tx * 3 + ty], path[x * 3 + y] = path[x * 3 + y], path[tx * 3 + ty]
pathStr = "".join(path)
if pathStr not in visited:
bfs.append((pathStr, step + 1))
path[tx * 3 + ty], path[x * 3 + y] = path[x * 3 + y], path[tx * 3 + ty]
return -1

def board2str(self, board):
bstr = ""
for i in range(2):
for j in range(3):
bstr += str(board[i][j])
return bstr
``````