# 1. Introduction

The Snake and Ladder game is a classic board game that involves players moving their pieces based on the roll of a die. Players traverse the board with the aim of reaching the last cell before their opponents. Along the way, they can land on ladders, which help them move faster by letting them climb ahead, or snakes, which set them back by sliding them to a lower position. The problem here is to find the minimum number of dice throws required to win the game, given a snake and ladder board.

# 2. Program Steps

1. Represent the board using an array, where each cell represents a vertex of a graph.

2. Create a graph where each vertex is connected to the next 6 vertices (6 faces of a die).

3. Use BFS (Breadth-First Search) to find the shortest path from the source (starting cell) to the destination (ending cell).

# 3. Code Program

```
import java.util.LinkedList;
import java.util.Queue;
class Cell {
int val;
int distance; // distance of this cell from the source cell
public Cell(int v, int d) {
val = v;
distance = d;
}
}
public class SnakeLadderProblem {
static int getMinDiceThrows(int[] moves, int N) {
boolean[] visited = new boolean[N];
Queue<Cell> queue = new LinkedList<>();
Cell src = new Cell(0, 0); // starting cell
queue.add(src);
while (!queue.isEmpty()) {
Cell curr = queue.poll();
int v = curr.val;
if (v == N - 1) {
break;
}
for (int j = v + 1; j <= v + 6 && j < N; j++) {
if (!visited[j]) {
visited[j] = true;
Cell cell = new Cell(j, curr.distance + 1);
if (moves[j] != -1) {
cell.val = moves[j];
}
queue.add(cell);
}
}
}
return src.distance;
}
public static void main(String[] args) {
int N = 30;
int[] moves = new int[N];
for (int i = 0; i < N; i++) {
moves[i] = -1;
}
// Add ladders
moves[2] = 21;
moves[4] = 7;
moves[10] = 25;
moves[19] = 28;
// Add snakes
moves[26] = 0;
moves[20] = 8;
moves[16] = 3;
moves[18] = 6;
System.out.println("Min Dice throws required is " + getMinDiceThrows(moves, N));
}
}
```

### Output:

Min Dice throws required is 3

# 4. Step By Step Explanation

1. The game board is represented as an array where the index represents the cell number and the value at each index represents the jump made from that cell. A value of -1 means no snake or ladder is present.

2. The idea is to use BFS on the board, treating each cell as a vertex. We traverse from the source (cell 0) and for each vertex, we consider the next 6 cells (possible dice outcomes) and enqueue them if they haven't been visited yet.

3. If a ladder or snake is found on a cell, the jump is made before enqueueing the new cell.

4. The BFS ensures we find the shortest path to the end of the board, representing the minimum dice throws required.