×

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player has complete control over the outcome of the dice throw and wishes to determine the smallest number of tosses necessary to reach the last cell.

If the player reaches a cell that is the base of a ladder, the player must climb that ladder, and if the player reaches a cell that is the mouth of the snake, the player must drop to the tail of the snake without a dice roll.

Take the board given as an example; it takes three minimum tosses of the dice to get from cell 1 to cell 30.

The steps are as follows:

  1. To go to cell number 3, roll two dice, and then climb the ladder to get to cell number 22.
  2. Then throw 6 more times to get to 28.
  3. Finally, I made it from 2 through 30.

Similar alternatives are (2, 2, 6), (2, 4, 4), and (2, 3, 5), among many others.

Consider the given snake and ladder board to be a directed graph with a number of vertices equal to the number of cells on the board. Finding the shortest path in a graph simplifies the problem. If the following six vertices do not have a snake or ladder, every vertex in the graph has an edge to them. If any of the following six vertices has a snake or a ladder, the edge from the current vertex will proceed to the top of the ladder or the tail of the snake. Because all edges have the same weight, we can use the graph's Breadth-First Search to discover the shortest path.

The execution of the following concept is shown below. The input is represented by two things: 'A,' which is the number of cells on the specified board, and an array'move[0...A-1]' of size A. If there is no snake or ladder from u, move[u] is -1; otherwise, move[u] includes the index of the destination cell for the snake or ladder at u.

Filename: SnakesLadder.java

// Java application for determining the smallest number of dice
// Throws are necessary to reach the last cell from the first.
// A given snake's cell with a ladder board
import java.util.LinkedList;
import java.util.Queue;
public class SnakesLadder {
	// A queue entry used in BFS
	static class qentry {
		int c; // Vertex number
		int dist; // The distance between this vertex and the source
	}
	// This method returns the smallest number of dice.
	// Throws are necessary to reach the last cell from the 0'th cell.
	// In a game of snakes and ladders. move[] is a collection of
	// size A, where A is the number of cells on board If there is
	// If there is no snake or ladder in cell u, then move[u].
	// is -1 Otherwise, move[u] includes the cell to which
	// u takes a snake or a ladder.
	static int getMinDiceThrows(int move[], int n)
	{
		int visited[] = new int[n];
		Queue<qentry> q = new LinkedList<>();
		qentry qe = new qentry();
		qe.c = 0;
		qe.dist = 0;
		// Enqueue and mark node 0 as visited.
		visited[0] = 1;
		q.add(qe);
		// Perform a BFS beginning from the vertex at index 0.
		while (!q.isEmpty()) {
			qe = q.remove();
			int c = qe.c;
			// If the destination is the front vertex
			// vertex, we are done
			if (c == n - 1)
				break;
			// Otherwise, remove the front vertex from the queue and
			// enqueue its neighbouring vertices (or cells
			// numbers that may be obtained by rolling a die)
			for (int u = c + 1; u <= (c + 6) && u < n;
				++u) {
				// If this cell has previously been visited,
				// ignore
				if (visited[u] == 0) {
					// Otherwise, determine its distance and
					// Register it as visited
					qentry a = new qentry();
					a.dist = (qe.dist + 1);
					visited[u] = 1;
					// Look for a snake or a ladder.
					// 'c' then snake's tail or top of
					// ladder is now near to 'u'
					if (move[u] != -1)
						a.c = move[u];
					else
						a.c = u;
					q.add(a);
				}
			}
		}
		// We arrive here when 'qe' has its last vertex.
		// return the vertex distance in 'qe'
		return qe.dist;
	}
	public static void main(String[] args)
	{
		// Let us build the board shown in the diagram above.
		int A = 30;
		int moves[] = new int[A];
		for (int u = 0; u < A; u++)
			moves[u] = -1;
		// Ladders
		moves[1] = 7;
		moves[5] = 21;
		moves[10] = 12;
		moves[20] = 15;
		// Snakes
		moves[26] = 0;
		moves[21] = 4;
		moves[14] = 9;
		moves[19] = 2;
		System.out.println("The Minimum number of dice rolls needed is "
						+ getMinDiceThrows(moves, A));
	}
}

Output:

Snake and Ladder Problem in Java

The following approach has an O(N) time complexity since each cell is added and deleted from the queue only once. And an enqueue or dequeue action typically takes O(1) time.

Another option is recursion, in which we travel to each block, in this example from 1 to 30, and keep a record of the minimum number of dice tosses at each block u and store it in an array t.

So, in general, we will:

  • Make an array, say 't,' and initialise it with -1.
  • Now we'll execute a recursive function from block 1 with a variable called u which we will increment.
  • Here also, we will define the base condition as when the block number reaches 30 or greater, we will return 0, and we will also check if this block has been visited before, which we will do by verifying the value of t[u], if this is -1, it means it has not been visited and we will proceed with the function, otherwise it has been visited and we will return the value of t[u].
  • Following the completion of the basic cases, we will initialise the variable 'min' with a maximum integer value.
  • Now, for every iteration, we would raise the value of u by the value of dice (eg: u+1,u+2....u+6) and verify whether any increment does have a ladder on it, if so, we will update the value of u to the end of the ladder and then send the value to the recursive function, if we don't have a ladder, we will pass the incremented value of u based on dice value to a recursive function; however, if we have a snake, we will not pass this value to a recursive function because we want to get to the end as soon as possible, and the best way to accomplish this is to avoid being eaten by a snake. And we'd keep updating the minimal value for the variable 'min'.
  • Finally, we will add min to t[u] and return t[u].

The following technique is implemented as follows:

Filename: SnakeLadder.java

import java.io.*;
import java.util.*;
class SnakeLadder {
	// Create an array t of length 31 that will be used from.
	// index to 1 to 30
	static int[] t = new int[31];
	static int minThrow(int n, int arr[])
	{
		for (int u = 0; u < 31; u++) {
			// the initialization of each index of t with -1
			t[u] = -1;
		}
		// establish a hashmap to store snakes and ladders
		// then ultimately, for more efficiency
		HashMap<Integer, Integer> h = new HashMap<>();
		for (int u = 0; u < 2 * n; u = u + 2) {
			// start as the key and end as the value
			h.put(arr[u], arr[u + 1]);
		}
		// final ans
		return sol(1, h);
	}
	//by using recursive function
	static int sol(int u, HashMap<Integer, Integer> h)
	{
		// base condintion
		if (u >= 30)
			return 0;
		// checking to see whether the block has previously been visited or
		// not(memoization).
		else if (t[u] != -1)
			return t[u];
		// establishing min as the maximum int value
		int min = Integer.MAX_VALUE;
		// for each dice value ranging from 1 to 6
		for (int v = 1; v <= 6; v++) {
			// incrementing the value of u with the value of the dice, i.e. v
			// introducing a new variable x
			//->using a new variable to avoid changing u
			// since we'll need it again in a later iteration
			int x = u + v;
			if (h.containsKey(x)) {
				// determining whether this is a snake of ladder or not
				// If it's a snake, we will be keep going because
				// need a snake
				if (h.get(x) < x)
					continue;
				// If it is a ladder to ladder end, it should be updated
				// value
				x = h.get(x);
			}
			// changing min in each iteration to obtain
			// from this specific block, the absolute minimum tosses
			min = Math.min(min, sol(x, h) + 1);
		}
		// changing the value of t[u] to min
		// memoization
		t[u] = min;
		return t[u];
	}
	// main
	public static void main(String[] args)
	{
		// Assuming a snakes and ladders board of 5x6,
		// You are given a number N that represents the total
		// There are several snakes and ladders, along with an array.
		// 2*N in size, with 2*u and (2*u + 1)th values
		// mark the beginning and end points, respectively
		// a snake or a ladder
		int N = 8;
		int[] arr = new int[2 * N];
		arr[0] = 4;
		arr[1] = 12;
		arr[2] = 8;
		arr[3] = 15;
		arr[4] = 6;
		arr[5] = 26;
		arr[6] = 2;
		arr[7] = 21;
		arr[8] = 14;
		arr[9] = 19;
		arr[10] = 12;
		arr[11] = 7;
		arr[12] = 26;
		arr[13] = 3;
		arr[14] = 29;
		arr[15] = 9;
		System.out.println("The Minimum number of dice rolls needed is "
						+ minThrow(N, arr));
	}
}

Output:

Snake and Ladder Problem in Java

Related Topics

Java file Writer

File Writer: It is used to write all the data from text files. It inherits the properties from the Output Stream class. It is meant for writing characters. It is meant...

4 minutes read.

Armstrong Number Program in Java

Armstrong Number Program in Java: A positive number is called an Armstrong number if the sum of the cube of each digit is equal to the number itself. There are...

6 minutes read.

Sierpinski Number in Java

The Sierpinski triangle—is it a fractal? The Sierpinski Triangle fractals. A self-similar fractal is the Sierpinski triangle. It is made of an equilateral triangle with its residual area successively reduced by...

3 minutes read.

Inheritance Program in Java

Inheritance Program in Java Inheritance is one of the important pillars of Object-Oriented Programming that facilitates parent-child relationships in programming. Using inheritance, we can create a new class with the help...

7 minutes read.

Java Math acos() Method

The acos() method of Math class computes the trigonometric Arc Cosine (inverse of cosine ) of an angle. The value returned is between 0.0 to pi. Syntax: public static double acos(double a) Parameters: The...

1 minute read.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

Features of Java

The objective of the Java programming language is to provide portability, security, and simplicity. In spite of this, Java has a number of features that makes it a popular language...

5 minutes read.

Stack Program in Java

Stack Program in Java The Stack class is part of the collection framework that inherits the Vector class. Thus, the Stack class can also be called a subclass of the Vector...

5 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

How to encrypt password in Java

Every software program needs a username and password to identify a legitimate user. A username can be any number of things, including an email address or a string of characters....

6 minutes read.

Empty Statement in Java

The three sorts of statements in Java are control, expression, and declaration statements. Additionally, another statement is referred to as an empty statement. In this section, we will discuss about...

4 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

Creating a Jar file in Java

The JDK's jar (Java Archive) tool offers the ability to produce jar files that can be executed. If you double-click a jar file that is executable, it will call the...

2 minutes read.

How to set timer in Java

In this article, you will be very well equipped with the knowledge to set timer in java. The timer in java can be set by using timer class provided by...

3 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

Set Up the Environment in Java

The Java is regarded as the pure object-oriented programming language. The Java applications are first compiled into the byte-code and then run by using the JVM. The Java is the...

4 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Interface in Java

  Interface in Java In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also...

6 minutes read.

Selection Sort in Java

Selection Sort in Java Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it...

5 minutes read.

Java Future Example

Future is an interface in the Java language that is a part of  java.util.concurrent package. It serves as a symbol for the output of an asynchronous computation. The interface offers ways to determine whether a computation has finished,  wait for it to finish, and receive its result. Once the task or calculation is finished, it cannot be undone. A Future interface offers ways to determine whether the computation is finished, to wait for it to finish, and to receive the computation's results. When the computation...

3 minutes read.