Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Snake and Ladder Problem
We know about the famous game Snake and Ladder. In this game, some rooms are present on the board, with the room number. Some rooms are connected with a ladder or with snakes. When we get a ladder, we can climb up to some rooms to reach near to the destination without moving sequentially. Similarly, when we get some snake, it sends us to a lower room to start the journey again from that room.

In this problem, we have to find the minimum number of the dice throw is required to reach start to destination.
Input and Output
Input: The starting and ending location of the snake and ladders. Snake: From 26 to 0, From 20 to 8, From 16 to 3, From 18 to 6 Ladder From 2 to 21, From 4 to 7, From 10 to 25, from 19 to 28 Output: Min Dice throws required is 3
Algorithm
minDiceThrow(move, cell)
Input: jump location for snake or ladder, and the total number of cells.
Output: Minimum number of dice throw required to reach to the final cell.
Begin initially mark all cell as unvisited define queue q mark the staring vertex as visited for starting vertex the vertex number := 0 and distance := 0 add starting vertex s into q while q is not empty, do qVert := front element of the queue v := vertex number of qVert if v = cell -1, then //when it is last vertex break the loop delete one item from queue for j := v + 1, to v + 6 and jExample
#include#include using namespace std; struct vertex { int vert; int dist; // Distance of this vertex from source }; int minDiceThrow(int move[], int cell) { bool visited[cell]; for (int i = 0; i q; visited[0] = true; //initially starting from 0 vertex s = {0, 0}; q.push(s); // Enqueue 0'th vertex vertex qVert; while (!q.empty()) { qVert = q.front(); int v = qVert.vert; if (v == cell-1) //when v is the destination vertex break; q.pop(); for (int j=v+1; j Output
Min Dice throws required is 3
Advertisements
