×

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 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Java Database Connectivity with Oracle

JDBC: A Programmer can develop a complete application using the Java built-in API’s. So, for storing the data required for solving a real-world problem is stored into a database. To connect...

5 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Convert Integer to Roman Numerals in Java

The main objective of this article is to convert the integers that are decimal values to the roman numbers. Problem statement: Write a software/program/code to convert any integer to a roman number. You...

10 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

How to resolve Illegal state exceptions in Java

What is IllegalStateException? When a method is called at the incorrect time, a runtime exception called an IllegalStateException is raised in Java. This exception is utilized to show that a method...

3 minutes read.

How to Send SMS in Java with Example

Sending SMS messages in Java is a fairly common task, and there are a number of libraries and APIs available to help you do it. One popular option is to...

2 minutes read.

Java Constant

A constant is an unchangeable entity in coding, as its title implies. The value which cannot be altered, in other terms. We shall understand about Java constants and exactly how...

3 minutes read.

Java 8 Features

Java 8 Features: After the great success of Java 7, many developers were waiting for the next version of one of the best languages in the tech world. Moreover, on...

6 minutes read.

Java String Methods

Java String Methods Java String class is the most important class of the java.lang package. It is used to handle the String related operations. It contains a lot of built-in Java...

2 minutes read.

Java String

Definition A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you...

4 minutes read.

Java Vs C++

Java Vs C++ Java and C++ both are Object Oriented Programming languages. Both languages are popular for competitive programming. C++ is used by many coders who have just started learning programming...

4 minutes read.

Missing Number in an Arithmetic Progression in Java

Given an array that shows the elements of an orderly arithmetic progression. Find the missing number to complete the succession of elements. Example:  Input: a [ ] = {2 , 4 , 6...

3 minutes read.

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.

Quick Sort in Java

Quick Sort in Java Like merge sort, quick sort also uses the divide and conquer approach to sort the given array or list. In quick sort, the sorting of an array...

6 minutes read.

Java Math scalb() Method

The scalb() method of Java Math class returns a single perfectly rounded product of floating-point and member of the double value set as if performed by d*2scaleFacor rounded value. Syntax: public static...

2 minutes read.

Java.lang.Exception.NoRunnableMethods

In Programming language, the java lang unexpected no precompiled methods error generally refers to a Junit exception that happens whenever Junit is incapable of locate the precompiled test methods. When...

4 minutes read.

Java Heap Space Out of Memory Error

This error is called an "out of memory" error, which indicates that the JVM cannot allocate an object in memory from the heap. Hence the java.lang.out of memory error, describing...

2 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.