773. Sliding Puzzle #875
-
Topics: On an The state of the board is solved if and only if the board is Given the puzzle board Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We can apply the Breadth-First Search (BFS) algorithm. The idea is to explore all possible configurations of the board starting from the given initial state, one move at a time, until we reach the solved state. Approach:
Let's implement this solution in PHP: 773. Sliding Puzzle <?php
/**
* @param Integer[][] $board
* @return Integer
*/
function slidingPuzzle($board) {
$target = "123450"; // The solved state as a string
$initial = implode('', array_map('implode', $board)); // Convert the board into a string
$queue = [[$initial, 0]]; // Queue for BFS, each element is a tuple (board_state, moves_count)
$visited = [$initial => true]; // Dictionary to track visited states
// Direction vectors for up, down, left, right movements
$directions = [-1, 1, -3, 3]; // Horizontal moves: -1 (left), 1 (right), Vertical moves: -3 (up), 3 (down)
while (!empty($queue)) {
list($current, $moves) = array_shift($queue); // Dequeue the current board state and move count
if ($current === $target) {
return $moves; // If the current state matches the solved state, return the number of moves
}
// Find the position of the empty space (0)
$zeroPos = strpos($current, '0');
foreach ($directions as $direction) {
$newPos = $zeroPos + $direction;
// Ensure the new position is valid and within bounds
if ($newPos >= 0 && $newPos < 6 && !($zeroPos % 3 == 0 && $direction == -1) && !($zeroPos % 3 == 2 && $direction == 1)) {
// Swap the '0' with the adjacent tile
$newState = $current;
$newState[$zeroPos] = $newState[$newPos];
$newState[$newPos] = '0';
if (!isset($visited[$newState])) {
$visited[$newState] = true;
array_push($queue, [$newState, $moves + 1]); // Enqueue the new state with incremented move count
}
}
}
}
return -1; // If BFS completes and we don't find the solution, return -1
}
// Test cases
$board1 = [[1, 2, 3], [4, 0, 5]];
echo slidingPuzzle($board1); // Output: 1
$board2 = [[1, 2, 3], [5, 4, 0]];
echo slidingPuzzle($board2); // Output: -1
$board3 = [[4, 1, 2], [5, 0, 3]];
echo slidingPuzzle($board3); // Output: 5
?> Explanation:
Time Complexity:
Space Complexity:
This solution should be efficient enough given the constraints. |
Beta Was this translation helpful? Give feedback.
We can apply the Breadth-First Search (BFS) algorithm. The idea is to explore all possible configurations of the board starting from the given initial state, one move at a time, until we reach the solved state.
Approach:
Breadth-First Search (BFS):
0
tile is swapped with an adjacent tile.State Representation: