VOOZH about

URL: https://www.geeksforgeeks.org/dsa/cses-solutions-swap-game/

⇱ CSES Solutions - Swap Game - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

CSES Solutions - Swap Game

Last Updated : 23 Jul, 2025

You are given a 3X3 grid containing the numbers 1,2...9. Your task is to perform a sequence of moves so that the grid will look like this:

1 2 3
4 5 6
7 8 9

On each move, you can swap the numbers in any two adjacent squares (horizontally or vertically). What is the minimum number of moves required?

Examples:

Input :

7 4 2
3 9 5
8 6 1

Output : 9

Explanation : The code transforms the initial grid 742395861 into the target grid 123456789 using BFS, then outputs the minimum number of moves required, which is 9

Input :

5 8 2
6 4 7
3 9 1

Output : 10

Explanation : The code takes the initial grid 582647391, applies BFS to transform it into the target grid 123456789, and outputs the minimum number of moves required, which is 10.

Approach :

Imagine the initial state of the puzzle as the starting point in a maze. You want to reach the goal state (the solved puzzle) at the other end. Breadth-First Search (BFS) explores this maze systematically. In order to run

Here's the intuition:

  • Explore closest neighbors first: BFS prioritizes exploring all possible moves from the current state (your location in the maze) before moving on to further away options. This ensures you find the quickest solution if it exists nearby.
  • Avoid revisiting dead ends: By keeping track of visited states, BFS avoids getting stuck in loops where you keep exploring the same configurations repeatedly. In order to keep track of visited states, we convert each board state into a number with base 9. Then, we start from the input grid and run the BFS till we reach the target grid.
  • Systematic exploration: BFS ensures all reachable states are explored in a layer-by-layer fashion. You move from the initial state to its immediate neighbors (one move away), then to their neighbors (two moves away), and so on, until the goal is found or determined unreachable.

Step-by-step algorithm:

  • Read the initial grid configuration from input.
  • Convert the grid into an integer representation.
  • Define the target configuration as an integer representation with base 9.
  • Initialize a queue for BFS traversal, a boolean array to mark visited states.
  • Enqueue the initial grid configuration with distance 0.
  • BFS Traversal:
    • While the queue is not empty:
    • Dequeue a grid configuration g along with its distance dist.
    • If g equals the target configuration, print dist and exit.
    • Generate all possible grids reachable from g by swapping adjacent pieces:
      • Horizontally adjacent swaps (ignore swaps involving the rightmost column).
      • Vertically adjacent swaps (ignore swaps involving the bottom row).
      • Mark generated grids as visited.
      • Enqueue unvisited grids with distance incremented by 1.
  • If the target configuration is not reached, print -1 (indicating it's unreachable).

This algorithm effectively explores the state space of grid configurations using BFS, generating all possible moves from each configuration until the target configuration is reached or determined unreachable.

Below is the implementation of the above algorithm:


Output
Minimum moves required: 9

Time Complexity: O((N * M)!), where N is the number of rows and M is the number of columns in the grid.
Auxiliary Space: O((N * M)!)

Comment
Article Tags:
Article Tags: