Javatpoint Logo
Javatpoint Logo

The Knight's Tour Problem in Java

The Knight's Tour Problem is a classic problem in computer science, mathematics, and chess. The Knight's Tour Problem involves finding a series of moves that a knight on a chessboard can make in order to visit every square only once. We will be developing a Java program that utilizes backtracking to solve this problem. The program will take an 8x8 chessboard as input and output a valid sequence of moves by the Knight that visits every square on the board exactly once. The solution will be found using recursive backtracking, a popular algorithmic technique for solving combinatorial problems. The Knight's Tour Problem is an interesting and challenging problem that will test your algorithmic skills and knowledge of Java programming.

Problem Statement:

Given a chessboard of size NxN and a knight placed on one of the squares, the task is to move the Knight to visit every square of the chessboard exactly once. The Knight can move according to the rules of chess; that is, it can move in an L-shape, with two squares in a horizontal or vertical direction followed by one square in a perpendicular direction. To solve the Knight's Tour Problem using Java, the solution should output a set of moves that the knight can take to cover every square on the board without repeating any square. If no solution is possible, the program should display a message notifying t.

Input :

Output:

0  59  38  33  30  17   8  63
37  34  31  60   9  62  29  16
58   1  36  39  32  27  18   7
35  48  41  26  61  10  15  28
42  57   2  49  40  23   6  19
47  50  45  54  25  20  11  14
56  43  52   3  22  13  24   5
51  46  55  44  53   4  21  12

The path followed by Knight to cover all the cells

The following is an 8x8 chessboard consisting of cells numbered with the move numbers of the knight.

0 59 38 33 30 17 8 63
37 34 31 60 9 62 29 16
58 1 36 39 32 27 18 7
35 48 41 26 61 10 15 28
42 57 2 49 40 23 6 19
47 50 45 54 25 20 11 14
56 43 52 3 22 13 24 5
51 46 55 44 53 4 21 12

Approach: Using Backtracking

In this approach, we start with an empty chessboard and try to place the Knight on each square in turn, backtracking when we reach a point where no further moves are possible. The Backtracking approach is a common algorithmic technique used to solve optimization problems, where we are searching for the best solution among many possible solutions.

Algorithm

  1. Define a static integer variable N representing the chessboard size (8).
  2. Define a Method isSafe(x, y, sol) to check if a knight can be placed at position (x, y) on the chessboard. The Method checks if (x, y) is within the board limits and if a knight does not already occupy the position.
  3. Define a Method print solution (sol) to print the final knight tour solution.
  4. Define a Method to solve KT () to solve the Knight's tour problem. a. Initialize an empty 2D array (sol) of size NxN, where -1 represents an empty cell. b. Define two arrays (xMove, yMove) to represent all possible knight moves. c. Set the starting position to (0,0) and mark it as visited by setting sol[0][0] to 0. d. Call the recursive Method solveKTUtil() with the starting position, movei=1, and the arrays xMove and yMove. e. If solveKTUtil() returns false, the Knight's tour solution does not exist. If it returns true, print the solution using printSolution() and return true.
  5. Define a recursive Method solveKTUtil(x, y, movie, sol, xMove, yMove) to solve the Knight's tour problem. a. If movie = N*N, the Knight's tour solution has been found, return true. b. Loop through all possible knight moves (k) using xMove and yMove arrays. c. Calculate the next position (next_x, next_y) using the current position (x, y) and the current knight move (k). d. If the next position is safe (i.e., isSafe(next_x, next_y, sol) returns true), mark it as visited by setting sol[next_x][next_y] = movei, and recursively call solveKTUtil() with the next position (next_x, next_y) and movei+1. e. If solveKTUtil() returns true, the Knight's tour solution has been found, return true. If it returns false, mark the next position as unvisited by setting sol[next_x][next_y] = -1 and continue to the next possible knight move. f. If no move leads to a solution, return false.
  6. The main method, called solveKT(), is to solve the Knight's tour problem. If a solution exists, it will be printed to the console.

Implementation

Filename: KnightTour.java

Output:

0 59 38 33 30 17 8 63 
37 34 31 60 9 62 29 16 
58 1 36 39 32 27 18 7 
35 48 41 26 61 10 15 28 
42 57 2 49 40 23 6 19 
47 50 45 54 25 20 11 14 
56 43 52 3 22 13 24 5 
51 46 55 44 53 4 21 12

Complexity Analysis: The time complexity of the Knight's tour problem using backtracking is O(8^(N^2)), where N is the size of the chessboard. It is because at each move, the Knight has 8 possible moves to choose from, and the total number of moves the Knight can make is N^2.

The space complexity of the program is O(N^2).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA