|
4 | 4 | import java.util.Queue;
|
5 | 5 | import java.util.Scanner;
|
6 | 6 |
|
7 |
| -// Java program to find minimum number of dice |
8 |
| -// throws required to reach last cell from first |
9 |
| -// cell of a given snake and ladder board |
| 7 | +// Java program to find the minimum number of dice throws required to reach |
| 8 | +// the last cell from the first cell of a given snake and ladder board. |
10 | 9 | public class SnakeAndLadder {
|
| 10 | + |
11 | 11 | public static class QueueEntry {
|
12 |
| - //cell number |
| 12 | + // Cell number |
13 | 13 | int cell;
|
14 |
| - //distance of the cell from source cell |
| 14 | + // Distance of the cell from source cell |
15 | 15 | int distance;
|
16 | 16 | }
|
17 | 17 |
|
18 | 18 | public static int getMinimumDiceThrows(int numberOfCells, int[] graph) {
|
19 |
| - //Array to keep track of which cells we have visited, 0 means unvisited, 1 means visited |
| 19 | + // Array to keep track of visited cells: 0 means unvisited, 1 means visited |
20 | 20 | int[] visited = new int[numberOfCells];
|
21 | 21 | for (int i = 0; i < numberOfCells; i++) {
|
22 | 22 | visited[i] = 0;
|
23 | 23 | }
|
24 |
| - //Inserting the source node in queue for BFS |
| 24 | + |
| 25 | + // Inserting the source node in queue for BFS |
25 | 26 | Queue<QueueEntry> queue = new LinkedList<>();
|
26 | 27 | QueueEntry startingQueueEntry = new QueueEntry();
|
27 | 28 | startingQueueEntry.cell = 0;
|
28 | 29 | startingQueueEntry.distance = 0;
|
29 | 30 | visited[0] = 1;
|
30 | 31 | queue.add(startingQueueEntry);
|
31 |
| - //Using BFS |
32 |
| - QueueEntry currentQueueEntry = null; |
| 32 | + |
| 33 | + // Using BFS |
| 34 | + QueueEntry currentQueueEntry; |
33 | 35 | while (!queue.isEmpty()) {
|
34 | 36 | currentQueueEntry = queue.remove();
|
35 |
| - //Our goal is to reach the ending cell, so we terminate the loop when it's reached |
| 37 | + |
| 38 | + // Our goal is to reach the ending cell |
36 | 39 | if (currentQueueEntry.cell == numberOfCells - 1) {
|
37 |
| - break; |
| 40 | + return currentQueueEntry.distance; |
38 | 41 | }
|
| 42 | + |
39 | 43 | for (int i = currentQueueEntry.cell + 1; i <= currentQueueEntry.cell + 6 && i < numberOfCells; i++) {
|
40 | 44 | if (visited[i] == 0) {
|
41 | 45 | visited[i] = 1;
|
42 | 46 | QueueEntry queueEntry = new QueueEntry();
|
43 | 47 | queueEntry.distance = currentQueueEntry.distance + 1;
|
44 | 48 |
|
45 |
| - if (graph[i] == -1) { |
46 |
| - queueEntry.cell = i; |
47 |
| - } else { |
48 |
| - queueEntry.cell = graph[i]; |
49 |
| - } |
| 49 | + // Check for snakes or ladders |
| 50 | + queueEntry.cell = (graph[i] == -1) ? i : graph[i]; |
50 | 51 | queue.add(queueEntry);
|
51 | 52 | }
|
52 | 53 | }
|
53 | 54 | }
|
54 |
| - return currentQueueEntry.distance; |
| 55 | + return -1; // Return -1 if the last cell is not reachable |
55 | 56 | }
|
56 | 57 |
|
57 | 58 | public static void main(String[] args) {
|
58 |
| - Scanner in = new Scanner(System.in); |
59 |
| - System.out.println("Enter number of cells in board"); |
60 |
| - int numberOfCells = in.nextInt(); |
| 59 | + Scanner scanner = new Scanner(System.in); |
| 60 | + System.out.print("Enter the number of cells on the board: "); |
| 61 | + int numberOfCells = scanner.nextInt(); |
61 | 62 |
|
62 |
| - //Representation of the board as graph |
63 |
| - //If there are no snakes or ladders starting from a cell, we mark it as -1 |
64 |
| - //If there is a snake or ladder, we will assign the ending position of the snake and ladder |
| 63 | + // Representation of the board as a graph |
65 | 64 | int[] graph = new int[numberOfCells];
|
66 | 65 | for (int i = 0; i < numberOfCells; i++) {
|
67 | 66 | graph[i] = -1;
|
68 | 67 | }
|
69 | 68 |
|
70 |
| - System.out.println("Enter number of snakes and ladders"); |
71 |
| - int n = in.nextInt(); |
72 |
| - System.out.println("Enter starting cell and ending cell of snake or ladder"); |
73 |
| - int[] positions = new int[2 * n]; |
| 69 | + System.out.print("Enter the number of snakes and ladders: "); |
| 70 | + int n = scanner.nextInt(); |
| 71 | + System.out.println("Enter the starting cell and ending cell of each snake or ladder:"); |
74 | 72 | for (int i = 0; i < n; i++) {
|
75 |
| - positions[2 * i] = in.nextInt(); |
76 |
| - positions[2 * i + 1] = in.nextInt(); |
77 |
| - //If there is a snake or ladder, we assign the ending position of the snake and ladder |
78 |
| - graph[positions[2 * i] - 1] = positions[2 * i + 1] - 1; |
| 73 | + int start = scanner.nextInt(); |
| 74 | + int end = scanner.nextInt(); |
| 75 | + // Assign the ending position of the snake or ladder |
| 76 | + graph[start - 1] = end - 1; |
| 77 | + } |
| 78 | + |
| 79 | + int result = getMinimumDiceThrows(numberOfCells, graph); |
| 80 | + if (result != -1) { |
| 81 | + System.out.println("Minimum number of dice throws required to end the game is " + result); |
| 82 | + } else { |
| 83 | + System.out.println("The last cell is not reachable."); |
79 | 84 | }
|
80 |
| - System.out.println("Minimum number of dice throws required to end the game is " + getMinimumDiceThrows(numberOfCells, graph)); |
| 85 | + |
| 86 | + scanner.close(); |
81 | 87 | }
|
82 | 88 | }
|
0 commit comments