3
3
import java .util .LinkedList ;
4
4
import java .util .Queue ;
5
5
import java .util .Scanner ;
6
+
6
7
// Java program to find minimum number of dice
7
8
// throws required to reach last cell from first
8
9
// cell of a given snake and ladder board
@@ -27,7 +28,6 @@ public static int getMinimumDiceThrows(int numberOfCells, int[] graph) {
27
28
startingQueueEntry .distance = 0 ;
28
29
visited [0 ] = 1 ;
29
30
queue .add (startingQueueEntry );
30
-
31
31
//Using BFS
32
32
QueueEntry currentQueueEntry = null ;
33
33
while (!queue .isEmpty ()) {
@@ -54,7 +54,7 @@ public static int getMinimumDiceThrows(int numberOfCells, int[] graph) {
54
54
return currentQueueEntry .distance ;
55
55
}
56
56
57
- public static void main (String args [] ) {
57
+ public static void main (String [] args ) {
58
58
Scanner in = new Scanner (System .in );
59
59
System .out .println ("Enter number of cells in board" );
60
60
int numberOfCells = in .nextInt ();
@@ -72,14 +72,11 @@ public static void main(String args[]) {
72
72
System .out .println ("Enter starting cell and ending cell of snake or ladder" );
73
73
int [] positions = new int [2 * n ];
74
74
for (int i = 0 ; i < n ; i ++) {
75
- int startingCell = in .nextInt ();
76
- int endingCell = in .nextInt ();
77
- positions [2 * i ] = startingCell ;
78
- positions [2 * i + 1 ] = endingCell ;
75
+ positions [2 * i ] = in .nextInt ();
76
+ positions [2 * i + 1 ] = in .nextInt ();
79
77
//If there is a snake or ladder, we assign the ending position of the snake and ladder
80
- graph [startingCell - 1 ] = endingCell - 1 ;
78
+ graph [positions [ 2 * i ] - 1 ] = positions [ 2 * i + 1 ] - 1 ;
81
79
}
82
80
System .out .println ("Minimum number of dice throws required to end the game is " + getMinimumDiceThrows (numberOfCells , graph ));
83
-
84
81
}
85
82
}
0 commit comments