Skip to content

Commit dbd5e82

Browse files
committed
formatting changes for clint
1 parent 6101d7f commit dbd5e82

File tree

1 file changed

+38
-32
lines changed

1 file changed

+38
-32
lines changed

src/main/java/com/thealgorithms/others/SnakeAndLadder.java

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,85 @@
44
import java.util.Queue;
55
import java.util.Scanner;
66

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.
109
public class SnakeAndLadder {
10+
1111
public static class QueueEntry {
12-
//cell number
12+
// Cell number
1313
int cell;
14-
//distance of the cell from source cell
14+
// Distance of the cell from source cell
1515
int distance;
1616
}
1717

1818
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
2020
int[] visited = new int[numberOfCells];
2121
for (int i = 0; i < numberOfCells; i++) {
2222
visited[i] = 0;
2323
}
24-
//Inserting the source node in queue for BFS
24+
25+
// Inserting the source node in queue for BFS
2526
Queue<QueueEntry> queue = new LinkedList<>();
2627
QueueEntry startingQueueEntry = new QueueEntry();
2728
startingQueueEntry.cell = 0;
2829
startingQueueEntry.distance = 0;
2930
visited[0] = 1;
3031
queue.add(startingQueueEntry);
31-
//Using BFS
32-
QueueEntry currentQueueEntry = null;
32+
33+
// Using BFS
34+
QueueEntry currentQueueEntry;
3335
while (!queue.isEmpty()) {
3436
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
3639
if (currentQueueEntry.cell == numberOfCells - 1) {
37-
break;
40+
return currentQueueEntry.distance;
3841
}
42+
3943
for (int i = currentQueueEntry.cell + 1; i <= currentQueueEntry.cell + 6 && i < numberOfCells; i++) {
4044
if (visited[i] == 0) {
4145
visited[i] = 1;
4246
QueueEntry queueEntry = new QueueEntry();
4347
queueEntry.distance = currentQueueEntry.distance + 1;
4448

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];
5051
queue.add(queueEntry);
5152
}
5253
}
5354
}
54-
return currentQueueEntry.distance;
55+
return -1; // Return -1 if the last cell is not reachable
5556
}
5657

5758
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();
6162

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
6564
int[] graph = new int[numberOfCells];
6665
for (int i = 0; i < numberOfCells; i++) {
6766
graph[i] = -1;
6867
}
6968

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:");
7472
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.");
7984
}
80-
System.out.println("Minimum number of dice throws required to end the game is " + getMinimumDiceThrows(numberOfCells, graph));
85+
86+
scanner.close();
8187
}
8288
}

0 commit comments

Comments
 (0)