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 on right side (taken from here), the minimum number of dice throws required to reach cell 30 from cell 1 is 3. Following are 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.

[ad type=”banner”]

We strongly recommend to minimize the browser and try this yourself first.
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 C++ 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.

C++ Programming:

[pastacode lang=”cpp” manual=”%2F%20C%2B%2B%20program%20to%20find%20minimum%20number%20of%20dice%20throws%20required%20to%0A%2F%2F%20reach%20last%20cell%20from%20first%20cell%20of%20a%20given%20snake%20and%20ladder%0A%2F%2F%20board%0A%23include%3Ciostream%3E%0A%23include%20%3Cqueue%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20An%20entry%20in%20queue%20used%20in%20BFS%0Astruct%20queueEntry%0A%7B%0A%20%20%20%20int%20v%3B%20%20%20%20%20%2F%2F%20Vertex%20number%0A%20%20%20%20int%20dist%3B%20%20%2F%2F%20Distance%20of%20this%20vertex%20from%20source%0A%7D%3B%0A%20%0A%2F%2F%20This%20function%20returns%20minimum%20number%20of%20dice%20throws%20required%20to%0A%2F%2F%20Reach%20last%20cell%20from%200’th%20cell%20in%20a%20snake%20and%20ladder%20game.%0A%2F%2F%20move%5B%5D%20is%20an%20array%20of%20size%20N%20where%20N%20is%20no.%20of%20cells%20on%20board%0A%2F%2F%20If%20there%20is%20no%20snake%20or%20ladder%20from%20cell%20i%2C%20then%20move%5Bi%5D%20is%20-1%0A%2F%2F%20Otherwise%20move%5Bi%5D%20contains%20cell%20to%20which%20snake%20or%20ladder%20at%20i%0A%2F%2F%20takes%20to.%0Aint%20getMinDiceThrows(int%20move%5B%5D%2C%20int%20N)%0A%7B%0A%20%20%20%20%2F%2F%20The%20graph%20has%20N%20vertices.%20Mark%20all%20the%20vertices%20as%0A%20%20%20%20%2F%2F%20not%20visited%0A%20%20%20%20bool%20*visited%20%3D%20new%20bool%5BN%5D%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20N%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20visited%5Bi%5D%20%3D%20false%3B%0A%20%0A%20%20%20%20%2F%2F%20Create%20a%20queue%20for%20BFS%0A%20%20%20%20queue%3CqueueEntry%3E%20q%3B%0A%20%0A%20%20%20%20%2F%2F%20Mark%20the%20node%200%20as%20visited%20and%20enqueue%20it.%0A%20%20%20%20visited%5B0%5D%20%3D%20true%3B%0A%20%20%20%20queueEntry%20s%20%3D%20%7B0%2C%200%7D%3B%20%20%2F%2F%20distance%20of%200’t%20vertex%20is%20also%200%0A%20%20%20%20q.push(s)%3B%20%20%2F%2F%20Enqueue%200’th%20vertex%0A%20%0A%20%20%20%20%2F%2F%20Do%20a%20BFS%20starting%20from%20vertex%20at%20index%200%0A%20%20%20%20queueEntry%20qe%3B%20%20%2F%2F%20A%20queue%20entry%20(qe)%0A%20%20%20%20while%20(!q.empty())%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20qe%20%3D%20q.front()%3B%0A%20%20%20%20%20%20%20%20int%20v%20%3D%20qe.v%3B%20%2F%2F%20vertex%20no.%20of%20queue%20entry%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20If%20front%20vertex%20is%20the%20destination%20vertex%2C%0A%20%20%20%20%20%20%20%20%2F%2F%20we%20are%20done%0A%20%20%20%20%20%20%20%20if%20(v%20%3D%3D%20N-1)%0A%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%0A%20%20%20%20%20%20%20%20%2F%2F%20Otherwise%20dequeue%20the%20front%20vertex%20and%20enqueue%0A%20%20%20%20%20%20%20%20%2F%2F%20its%20adjacent%20vertices%20(or%20cell%20numbers%20reachable%0A%20%20%20%20%20%20%20%20%2F%2F%20through%20a%20dice%20throw)%0A%20%20%20%20%20%20%20%20q.pop()%3B%0A%20%20%20%20%20%20%20%20for%20(int%20j%3Dv%2B1%3B%20j%3C%3D(v%2B6)%20%26%26%20j%3CN%3B%20%2B%2Bj)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20If%20this%20cell%20is%20already%20visited%2C%20then%20ignore%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20(!visited%5Bj%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Otherwise%20calculate%20its%20distance%20and%20mark%20it%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20as%20visited%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20queueEntry%20a%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20a.dist%20%3D%20(qe.dist%20%2B%201)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bj%5D%20%3D%20true%3B%0A%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Check%20if%20there%20a%20snake%20or%20ladder%20at%20’j’%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20then%20tail%20of%20snake%20or%20top%20of%20ladder%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20become%20the%20adjacent%20of%20’i’%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(move%5Bj%5D%20!%3D%20-1)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20a.v%20%3D%20move%5Bj%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20else%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20a.v%20%3D%20j%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20q.push(a)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20We%20reach%20here%20when%20’qe’%20has%20last%20vertex%0A%20%20%20%20%2F%2F%20return%20the%20distance%20of%20vertex%20in%20’qe’%0A%20%20%20%20return%20qe.dist%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20methods%20of%20graph%20class%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Let%20us%20construct%20the%20board%20given%20in%20above%20diagram%0A%20%20%20%20int%20N%20%3D%2030%3B%0A%20%20%20%20int%20moves%5BN%5D%3B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%3CN%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20moves%5Bi%5D%20%3D%20-1%3B%0A%20%0A%20%20%20%20%2F%2F%20Ladders%0A%20%20%20%20moves%5B2%5D%20%3D%2021%3B%0A%20%20%20%20moves%5B4%5D%20%3D%207%3B%0A%20%20%20%20moves%5B10%5D%20%3D%2025%3B%0A%20%20%20%20moves%5B19%5D%20%3D%2028%3B%0A%20%0A%20%20%20%20%2F%2F%20Snakes%0A%20%20%20%20moves%5B26%5D%20%3D%200%3B%0A%20%20%20%20moves%5B20%5D%20%3D%208%3B%0A%20%20%20%20moves%5B16%5D%20%3D%203%3B%0A%20%20%20%20moves%5B18%5D%20%3D%206%3B%0A%20%0A%20%20%20%20cout%20%3C%3C%20%22Min%20Dice%20throws%20required%20is%20%22%20%3C%3C%20getMinDiceThrows(moves%2C%20N)%3B%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:

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.

[ad type=”banner”]