Given a snake and ladder board, find the minimum number of dice throws required to reach the destination or last cell from source or 1st cell. Basically, the player has total control over outcome of dice throw and wants to find out minimum number of throws required to reach last cell.
If the player reaches a cell which is base of a ladder, the player has to climb up that ladder and if reaches a cell is mouth of the snake, has to go down to the tail of snake without a dice throw.
For example, consider the board shown, the minimum number of dice throws required to reach cell 30 from cell 1 is 3.
Following are the steps:
a) First throw two on dice to reach cell number 3 and then ladder to reach 22
b) Then throw 6 to reach 28.
c) Finally through 2 to reach 30.
There can be other solutions as well like (2, 2, 6), (2, 4, 4), (2, 3, 5).. etc.
The idea is to consider the given snake and ladder board as a directed graph with number of vertices equal to the number of cells in the board. The problem reduces to finding the shortest path in a graph. Every vertex of the graph has an edge to next six vertices if next 6 vertices do not have a snake or ladder. If any of the next six vertices has a snake or ladder, then the edge from current vertex goes to the top of the ladder or tail of the snake. Since all edges are of equal weight, we can efficiently find shortest path using Breadth First Search of the graph.
Following is the implementation of the above idea. The input is represented by two things, first is ‘N’ which is number of cells in the given board, second is an array ‘move[0…N-1]’ of size N. An entry move[i] is -1 if there is no snake and no ladder from i, otherwise move[i] contains index of destination cell for the snake or the ladder at i.
Min Dice throws required is 3
Time complexity of the above solution is O(N) as every cell is added and removed only once from queue. And a typical enqueue or dequeue operation takes O(1) time.
This article is contributed by Siddharth. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Word Ladder - Set 2 ( Bi-directional BFS )
- Word Ladder (Length of shortest chain to reach a target word)
- Nuts & Bolts Problem (Lock & Key problem) | Set 1
- Nuts & Bolts Problem (Lock & Key problem) | Set 2 (Hashmap)
- Travelling Salesman Problem | Set 1 (Naive and Dynamic Programming)
- Vertex Cover Problem | Set 1 (Introduction and Approximate Algorithm)
- Applications of Minimum Spanning Tree Problem
- The Knight's tour problem | Backtracking-1
- N Queen Problem | Backtracking-3
- Activity Selection Problem | Greedy Algo-1
- m Coloring Problem | Backtracking-5
- Box Stacking Problem | DP-22
- Subset Sum Problem | DP-25
- The Stock Span Problem
- Tree Isomorphism Problem
- Ford-Fulkerson Algorithm for Maximum Flow Problem
- Word Break Problem | DP-32
- Stable Marriage Problem
- Travelling Salesman Problem | Set 2 (Approximate using MST)
- Printing brackets in Matrix Chain Multiplication Problem
Improved By : 29AjayKumar