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