773. Sliding Puzzle Hard
1/**
2 * [773] Sliding Puzzle
3 *
4 * On an 2 x 3 board, there are five tiles labeled from 1 to 5, and an empty square represented by 0. A move consists of choosing 0 and a 4-directionally adjacent number and swapping it.
5 * The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]].
6 * Given the puzzle board 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.
7 *
8 * Example 1:
9 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/slide1-grid.jpg" style="width: 244px; height: 165px;" />
10 * Input: board = [[1,2,3],[4,0,5]]
11 * Output: 1
12 * Explanation: Swap the 0 and the 5 in one move.
13 *
14 * Example 2:
15 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/slide2-grid.jpg" style="width: 244px; height: 165px;" />
16 * Input: board = [[1,2,3],[5,4,0]]
17 * Output: -1
18 * Explanation: No number of moves will make the board solved.
19 *
20 * Example 3:
21 * <img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/slide3-grid.jpg" style="width: 244px; height: 165px;" />
22 * Input: board = [[4,1,2],[5,0,3]]
23 * Output: 5
24 * Explanation: 5 is the smallest number of moves that solves the board.
25 * An example path:
26 * After move 0: [[4,1,2],[5,0,3]]
27 * After move 1: [[4,1,2],[0,5,3]]
28 * After move 2: [[0,1,2],[4,5,3]]
29 * After move 3: [[1,0,2],[4,5,3]]
30 * After move 4: [[1,2,0],[4,5,3]]
31 * After move 5: [[1,2,3],[4,5,0]]
32 *
33 *
34 * Constraints:
35 *
36 * board.length == 2
37 * board[i].length == 3
38 * 0 <= board[i][j] <= 5
39 * Each value board[i][j] is unique.
40 *
41 */
42pub struct Solution {}
43
44// problem: https://leetcode.com/problems/sliding-puzzle/
45// discuss: https://leetcode.com/problems/sliding-puzzle/discuss/?currentPage=1&orderBy=most_votes&query=
46
47// submission codes start here
48
49impl Solution {
50 pub fn sliding_puzzle(board: Vec<Vec<i32>>) -> i32 {
51 0
52 }
53}
54
55// submission codes end
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 #[test]
62 fn test_773() {
63 }
64}
65
Back
© 2025 bowen.ge All Rights Reserved.