|
| 1 | +<h2><a href="https://leetcode.com/problems/sliding-puzzle/">773. Sliding Puzzle</a></h2><h3>Hard</h3><hr><div><p>On an <code>2 x 3</code> board, there are five tiles labeled from <code>1</code> to <code>5</code>, and an empty square represented by <code>0</code>. A <strong>move</strong> consists of choosing <code>0</code> and a 4-directionally adjacent number and swapping it.</p> |
| 2 | + |
| 3 | +<p>The state of the board is solved if and only if the board is <code>[[1,2,3],[4,5,0]]</code>.</p> |
| 4 | + |
| 5 | +<p>Given the puzzle board <code>board</code>, return <em>the least number of moves required so that the state of the board is solved</em>. If it is impossible for the state of the board to be solved, return <code>-1</code>.</p> |
| 6 | + |
| 7 | +<p> </p> |
| 8 | +<p><strong class="example">Example 1:</strong></p> |
| 9 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/slide1-grid.jpg" style="width: 244px; height: 165px;"> |
| 10 | +<pre><strong>Input:</strong> board = [[1,2,3],[4,0,5]] |
| 11 | +<strong>Output:</strong> 1 |
| 12 | +<strong>Explanation:</strong> Swap the 0 and the 5 in one move. |
| 13 | +</pre> |
| 14 | + |
| 15 | +<p><strong class="example">Example 2:</strong></p> |
| 16 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/slide2-grid.jpg" style="width: 244px; height: 165px;"> |
| 17 | +<pre><strong>Input:</strong> board = [[1,2,3],[5,4,0]] |
| 18 | +<strong>Output:</strong> -1 |
| 19 | +<strong>Explanation:</strong> No number of moves will make the board solved. |
| 20 | +</pre> |
| 21 | + |
| 22 | +<p><strong class="example">Example 3:</strong></p> |
| 23 | +<img alt="" src="https://assets.leetcode.com/uploads/2021/06/29/slide3-grid.jpg" style="width: 244px; height: 165px;"> |
| 24 | +<pre><strong>Input:</strong> board = [[4,1,2],[5,0,3]] |
| 25 | +<strong>Output:</strong> 5 |
| 26 | +<strong>Explanation:</strong> 5 is the smallest number of moves that solves the board. |
| 27 | +An example path: |
| 28 | +After move 0: [[4,1,2],[5,0,3]] |
| 29 | +After move 1: [[4,1,2],[0,5,3]] |
| 30 | +After move 2: [[0,1,2],[4,5,3]] |
| 31 | +After move 3: [[1,0,2],[4,5,3]] |
| 32 | +After move 4: [[1,2,0],[4,5,3]] |
| 33 | +After move 5: [[1,2,3],[4,5,0]] |
| 34 | +</pre> |
| 35 | + |
| 36 | +<p> </p> |
| 37 | +<p><strong>Constraints:</strong></p> |
| 38 | + |
| 39 | +<ul> |
| 40 | + <li><code>board.length == 2</code></li> |
| 41 | + <li><code>board[i].length == 3</code></li> |
| 42 | + <li><code>0 <= board[i][j] <= 5</code></li> |
| 43 | + <li>Each value <code>board[i][j]</code> is <strong>unique</strong>.</li> |
| 44 | +</ul> |
| 45 | +</div> |
0 commit comments