leetcode-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]] |
Note:
board
will be a 2 x 3 array as described above.board[i][j]
will be a permutation of[0, 1, 2, 3, 4, 5]
.
分析
将board转化为状态然后进行广度优先搜索。
代码1
1 | // 20ms |
代码2
1 | // 4ms |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/15/leetcode-773/
License: 知识共享署名-非商业性使用 4.0 国际许可协议