VOOZH about

URL: https://www.geeksforgeeks.org/dsa/snake-ladder-problem-2/

⇱ Snake and Ladder Problem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Snake and Ladder Problem

Last Updated : 23 Jul, 2025

Given a snake and ladder board, find the minimum number of dice throws required to reach the destination or last cell from the source or 1st cell. Basically, the player has total control over the outcome of the dice throw and wants to find out the minimum number of throws required to reach the last cell.
If the player reaches a cell which is the base of a ladder, the player has to climb up that ladder and if reaches a cell is the mouth of the snake, and has to go down to the tail of the snake without a dice throw.

Example:

Input:

👁 Snake-and-Ladder-Problem

Output: 3
Explanation: Following are the steps:

  • First throw two dice to reach cell number 3 and then ladder to reach 22 
  • Then throw 6 to reach 28.
  • Finally through 2 to reach 30.
  • There can be other solutions as well like (2, 2, 6), (2, 4, 4), (2, 3, 5).. etc.

[Approach - 1] Using Breadth-First Search - O(n) Time and O(n) Space

The idea is to model the Snakes and Ladders board as a graph where each cell is a node and dice throws represent edges. The thought process is to use Breadth-First Search (BFS) since we're looking for the shortest path (minimum number of moves) from start to end. An important observation is that when you land on a cell with a ladder or snake, you're teleported to a new cell, this effectively changes the destination node of that edge. BFS explores all possible dice rolls from each cell in the shortest-first manner, ensuring we reach the final cell with the least number of throws.

Steps to implement the above idea:

  • Create a visited array to keep track of visited cells during the BFS traversal of the board.
  • Initialize a queue that stores pairs of current cell index and the number of moves taken.
  • Begin from cell 0 with distance 0, mark it visited, and push into the queue as the starting point.
  • While the queue is not empty, extract the front element and get its current cell and distance.
  • If the current cell is the last one, return the distance as it represents minimum dice throws.
  • Loop through all possible next 6 cells using a dice roll and for each, check snake or ladder jump.
  • If the destination cell is not visited, mark it visited and enqueue it with distance incremented by 1.

Output
3

[Approach - 2] Using Depth-First Search - O(n) Time and O(n) Space

The idea is to simulate all possible dice throws using Depth First Search (DFS) to explore every path from start to finish. The thought process is to move from the current position to all possible next positions (via dice throw 1–6), jumping to a new location if there's a snake or ladder. An important observation is to prune paths that are already worse than a previously found one using a visited map that tracks minimum moves at each cell. This ensures we only pursue optimal paths, and stop early when a shorter one is already found.

Steps to implement the above idea:

  • Define a recursive function that explores all possible paths from the current position with a move counter.
  • Use a map to track visited positions and the least number of moves needed to reach each of them.
  • Add a base case to update the minimum moves if the current path reaches the final destination.
  • Prune the recursion if the current path takes more moves than already recorded for the same position.
  • For each call, simulate all dice throws from 1 to 6 and compute the next position accordingly.
  • If the next cell has a ladder or snake, jump to its destination before making the recursive call.
  • Start the function from position 0, and return -1 if no valid path reaches the end of the board.

Output
3


Comment