Skip to content
Related Articles

Related Articles

Improve Article

Print all Knight’s tour possible from a starting point on NxN chessboard

  • Last Updated : 13 Oct, 2021

Given a N x N chessboard with a Knight initially standing on the Xth row and Yth column, the task is to print all possible paths such that the knight must visit each square exactly once.

Example:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: N = 5, X = 1, Y = 1
Output: 
1 6 15 10 21 
14 9 20 5 16 
19 2 7 22 11 
8 13 24 17 4 
25 18 3 12 23 



1 6 11 18 21 
12 17 20 5 10 
7 2 15 22 19 
16 13 24 9 4 
25 8 3 14 23 

… 302 more
Explanation: Initially, the knight is at (1, 2)th cell. According to the 1st path, the knight will visit the cells in the following order: (1, 1) -> (3, 2) -> (5, 3) -> (4, 5) -> (2, 4) … and so on.

Input: N = 3, X = 1, Y = 3
Output: -1
Explanation: There exist no valid sequence of path such that the knight visit each square exactly once.

Approach: The problem can be solved with the help of Recursion and Backtracking by generating all the possible tours one by one and checking if it satisfies the given conditions. A more thorough explanation of the similar approach is discussed in the Knight’s Tour Problem. Below are the steps to follow:

  • Create a Recursive function to iterate over all possible paths that the Knight can follow.
  • Maintain the number of squares visited by the Knight using a variable visited.
  • Create a function isSafe() which takes the coordinates of a square as an argument and returns whether the square is valid for the next Knight’s move.
  • Iterate through all the possible 8 moves of the Knight and check whether they are safe to visit. If they are, recursively call for the next move until the number of visited squares is equal to the total number of squares.

Below is the implementation of the above approach:

C++




// C++ program of the above approach
#include <bits/stdc++.h>
using namespace std;
  
// Stores the 8 possible combinations of
// moves that the knight can follow
int DirX[] = { 2, 1, -1, -2, -2, -1, 1, 2 };
int DirY[] = { 1, 2, 2, 1, -1, -2, -2, -1 };
  
// Function to find if (i, j) is a valid
// cell for the knight to move and it
// exists within the chessboard
bool isSafe(int i, int j, int n,
            vector<vector<int> >& Board)
{
    return (i >= 0 and j >= 0 and i < n and j < n
            and Board[i][j] == 0);
}
  
// Stores whether there exist any valid path
bool isPossible = false;
  
// Recursive function to iterate through all
// the paths that the knight can follow
void knightTour(vector<vector<int> >& ChessBoard, int N,
                int x, int y, int visited = 1)
{
    // Mark the current square of the chessboard
    ChessBoard[x][y] = visited;
  
    // If the number of visited squares are equal
    // to the total number of sqares
    if (visited == N * N) {
        isPossible = true;
  
        // Print the current state of ChessBoard
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                cout << ChessBoard[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
  
        // Backtrack to the previous move
        ChessBoard[x][y] = 0;
        return;
    }
  
    // Iterate through all the eight possible moves
    // for a knight
    for (int i = 0; i < 8; i++) {
  
        // Stores the new position of the knight
        // after a move
        int newX = x + DirX[i];
        int newY = y + DirY[i];
  
        // If the new position is a valid position
        // recursively call for the next move
        if (isSafe(newX, newY, N, ChessBoard)
            && !ChessBoard[newX][newY]) {
            knightTour(ChessBoard, N, newX, newY,
                       visited + 1);
        }
    }
  
    // Backtrack to the previous move
    ChessBoard[x][y] = 0;
}
  
// Driver Code
int main()
{
    vector<vector<int> > ChessBoard(5, vector<int>(5, 0));
    int N = ChessBoard.size();
    int X = 1;
    int Y = 1;
  
    knightTour(ChessBoard, N, X - 1, Y - 1);
  
    // If no valid sequence of moves exist
    if (!isPossible) {
        cout << -1;
    }
  
    return 0;
}
Output
1 6 15 10 21 
14 9 20 5 16 
19 2 7 22 11 
8 13 24 17 4 
25 18 3 12 23 

1 6 11 18 21 
12 17 20 5 10 
7 2 15 22 19 
16 13 24 9 4 
25 8 3 14 23 

1 6 11 16 21 
12 15 20 5 10 
7 2 13 22 17 
14 19 24 9 4 
25 8 3 18 23 

1 6 17 12 21 
16 11 20 5 18 
7 2 9 22 13 
10 15 24 19 4 
25 8 3 14 23 

1 12 17 6 21 
18 5 20 11 16 
13 2 9 22 7 
4 19 24 15 10 
25 14 3 8 23 

1 16 11 6 21 
10 5 20 15 12 
17 2 13 22 7 
4 9 24 19 14 
25 18 3 8 23 

1 18 11 6 21 
10 5 20 17 12 
19 2 15 22 7 
4 9 24 13 16 
25 14 3 8 23 

1 10 15 6 21 
16 5 20 9 14 
11 2 7 22 19 
4 17 24 13 8 
25 12 3 18 23 

1 16 5 10 21 
6 11 20 15 4 
19 2 17 22 9 
12 7 24 3 14 
25 18 13 8 23 

1 12 5 18 21 
6 17 20 13 4 
11 2 9 22 19 
16 7 24 3 14 
25 10 15 8 23 

1 10 5 16 21 
6 15 20 11 4 
9 2 7 22 17 
14 19 24 3 12 
25 8 13 18 23 

1 18 7 12 21 
8 13 20 17 6 
19 2 5 22 11 
14 9 24 3 16 
25 4 15 10 23 

1 6 17 12 21 
18 11 20 7 16 
5 2 15 22 13 
10 19 24 3 8 
25 4 9 14 23 

1 6 15 12 21 
16 11 20 7 14 
5 2 13 22 19 
10 17 24 3 8 
25 4 9 18 23 

1 12 17 8 21 
18 7 20 3 16 
13 2 11 22 9 
6 19 24 15 4 
25 14 5 10 23 

1 16 13 8 21 
12 7 20 3 14 
17 2 15 22 9 
6 11 24 19 4 
25 18 5 10 23 

1 18 13 8 21 
12 7 20 3 14 
19 2 17 22 9 
6 11 24 15 4 
25 16 5 10 23 

1 10 15 8 21 
16 7 20 3 14 
11 2 9 22 19 
6 17 24 13 4 
25 12 5 18 23 

1 4 15 10 21 
14 9 20 3 16 
19 2 5 22 11 
8 13 24 17 6 
25 18 7 12 23 

1 4 9 18 21 
10 17 20 3 8 
5 2 13 22 19 
16 11 24 7 14 
25 6 15 12 23 

1 4 9 16 21 
10 15 20 3 8 
5 2 11 22 17 
14 19 24 7 12 
25 6 13 18 23 

1 4 17 12 21 
16 11 20 3 18 
5 2 7 22 13 
10 15 24 19 8 
25 6 9 14 23 

1 16 3 10 21 
6 11 20 15 4 
17 2 5 22 9 
12 7 24 19 14 
25 18 13 8 23 

1 18 3 12 21 
8 13 20 17 4 
19 2 7 22 11 
14 9 24 5 16 
25 6 15 10 23 

1 8 3 14 21 
18 13 20 9 4 
7 2 17 22 15 
12 19 24 5 10 
25 6 11 16 23 

1 8 3 14 21 
16 13 20 9 4 
7 2 15 22 19 
12 17 24 5 10 
25 6 11 18 23 

1 14 3 8 21 
4 9 20 13 16 
19 2 15 22 7 
10 5 24 17 12 
25 18 11 6 23 

1 14 3 8 21 
4 9 20 13 18 
15 2 17 22 7 
10 5 24 19 12 
25 16 11 6 23 

1 12 3 18 21 
4 17 20 13 8 
11 2 7 22 19 
16 5 24 9 14 
25 10 15 6 23 

1 10 3 16 21 
4 15 20 11 6 
9 2 5 22 17 
14 19 24 7 12 
25 8 13 18 23 

1 22 11 16 7 
12 17 8 21 10 
25 2 23 6 15 
18 13 4 9 20 
3 24 19 14 5 

1 22 11 16 7 
12 17 8 21 10 
23 2 25 6 15 
18 13 4 9 20 
3 24 19 14 5 

1 24 11 16 7 
12 17 8 25 10 
23 2 21 6 15 
18 13 4 9 20 
3 22 19 14 5 

1 22 11 16 7 
12 17 8 23 10 
21 2 19 6 15 
18 13 4 9 24 
3 20 25 14 5 

1 20 11 16 7 
12 25 8 21 10 
19 2 17 6 15 
24 13 4 9 22 
3 18 23 14 5 

1 18 11 24 7 
12 23 8 19 10 
17 2 15 6 25 
22 13 4 9 20 
3 16 21 14 5 

1 16 11 22 7 
12 21 8 17 10 
15 2 13 6 23 
20 25 4 9 18 
3 14 19 24 5 

1 14 25 20 7 
24 19 8 15 10 
13 2 11 6 21 
18 23 4 9 16 
3 12 17 22 5 

1 24 13 18 7 
14 19 8 23 12 
25 2 11 6 17 
20 15 4 9 22 
3 10 21 16 5 

1 12 15 20 7 
16 21 8 25 14 
11 2 13 6 19 
22 17 4 9 24 
3 10 23 18 5 

1 12 17 22 7 
18 23 8 13 16 
11 2 15 6 21 
24 19 4 9 14 
3 10 25 20 5 

1 12 19 24 7 
20 25 8 13 18 
11 2 17 6 23 
16 21 4 9 14 
3 10 15 22 5 

1 12 23 18 7 
24 17 8 13 22 
11 2 21 6 19 
16 25 4 9 14 
3 10 15 20 5 

1 12 25 18 7 
22 17 8 13 24 
11 2 23 6 19 
16 21 4 9 14 
3 10 15 20 5 

1 12 23 18 7 
22 17 8 13 24 
11 2 25 6 19 
16 21 4 9 14 
3 10 15 20 5 

1 12 21 18 7 
22 17 8 13 20 
11 2 19 6 25 
16 23 4 9 14 
3 10 15 24 5 

1 16 21 10 7 
20 11 8 15 22 
25 2 17 6 9 
12 19 4 23 14 
3 24 13 18 5 

1 16 21 10 7 
22 11 8 15 20 
17 2 25 6 9 
12 23 4 19 14 
3 18 13 24 5 

1 16 21 10 7 
22 11 8 15 20 
17 2 23 6 9 
12 25 4 19 14 
3 18 13 24 5 

1 16 25 10 7 
24 11 8 15 20 
17 2 21 6 9 
12 23 4 19 14 
3 18 13 22 5 

1 16 23 10 7 
22 11 8 15 24 
17 2 19 6 9 
12 21 4 25 14 
3 18 13 20 5 

1 24 19 10 7 
18 11 8 25 20 
23 2 15 6 9 
12 17 4 21 14 
3 22 13 16 5 

1 22 17 10 7 
16 11 8 23 18 
21 2 13 6 9 
12 15 4 19 24 
3 20 25 14 5 

1 20 15 10 7 
14 25 8 21 16 
19 2 11 6 9 
24 13 4 17 22 
3 18 23 12 5 

1 18 23 12 7 
24 13 8 17 22 
19 2 11 6 9 
14 25 4 21 16 
3 20 15 10 5 

1 20 25 14 7 
12 15 8 19 24 
21 2 13 6 9 
16 11 4 23 18 
3 22 17 10 5 

1 22 13 16 7 
12 17 8 21 14 
23 2 15 6 9 
18 11 4 25 20 
3 24 19 10 5 

1 24 13 18 7 
12 19 8 23 14 
25 2 17 6 9 
20 11 4 15 22 
3 16 21 10 5 

1 18 13 20 7 
12 21 8 25 14 
17 2 19 6 9 
22 11 4 15 24 
3 16 23 10 5 

1 18 13 22 7 
12 23 8 19 14 
17 2 21 6 9 
24 11 4 15 20 
3 16 25 10 5 

1 18 13 24 7 
12 25 8 19 14 
17 2 23 6 9 
22 11 4 15 20 
3 16 21 10 5 

1 18 13 24 7 
12 23 8 19 14 
17 2 25 6 9 
22 11 4 15 20 
3 16 21 10 5 

1 24 13 18 7 
14 19 8 23 12 
9 2 25 6 17 
20 15 4 11 22 
3 10 21 16 5 

1 24 13 18 7 
14 19 8 25 12 
9 2 23 6 17 
20 15 4 11 22 
3 10 21 16 5 

1 22 13 18 7 
14 19 8 23 12 
9 2 21 6 17 
20 15 4 11 24 
3 10 25 16 5 

1 20 13 18 7 
14 25 8 21 12 
9 2 19 6 17 
24 15 4 11 22 
3 10 23 16 5 

1 18 13 24 7 
14 23 8 19 12 
9 2 17 6 25 
22 15 4 11 20 
3 10 21 16 5 

1 16 13 22 7 
14 21 8 17 12 
9 2 15 6 23 
20 25 4 11 18 
3 10 19 24 5 

1 14 25 20 7 
24 19 8 15 12 
9 2 13 6 21 
18 23 4 11 16 
3 10 17 22 5 

1 12 23 18 7 
22 17 8 13 24 
9 2 11 6 19 
16 21 4 25 14 
3 10 15 20 5 

1 10 15 20 7 
16 21 8 25 14 
9 2 11 6 19 
22 17 4 13 24 
3 12 23 18 5 

1 10 17 22 7 
18 23 8 11 16 
9 2 13 6 21 
24 19 4 15 12 
3 14 25 20 5 

1 10 19 24 7 
20 25 8 11 18 
9 2 15 6 23 
14 21 4 17 12 
3 16 13 22 5 

1 10 23 16 7 
24 15 8 11 22 
9 2 19 6 17 
14 25 4 21 12 
3 20 13 18 5 

1 10 25 16 7 
20 15 8 11 24 
9 2 21 6 17 
14 19 4 23 12 
3 22 13 18 5 

1 10 21 16 7 
20 15 8 11 22 
9 2 23 6 17 
14 19 4 25 12 
3 24 13 18 5 

1 10 21 16 7 
20 15 8 11 22 
9 2 25 6 17 
14 19 4 23 12 
3 24 13 18 5 

1 10 21 16 7 
22 15 8 11 20 
9 2 17 6 25 
14 23 4 19 12 
3 18 13 24 5 

1 18 21 12 7 
20 13 8 17 22 
25 2 19 6 11 
14 9 4 23 16 
3 24 15 10 5 

1 18 23 12 7 
24 13 8 17 22 
19 2 25 6 11 
14 9 4 21 16 
3 20 15 10 5 

1 18 25 12 7 
24 13 8 17 22 
19 2 23 6 11 
14 9 4 21 16 
3 20 15 10 5 

1 18 23 12 7 
22 13 8 17 24 
19 2 21 6 11 
14 9 4 25 16 
3 20 15 10 5 

1 24 19 12 7 
18 13 8 25 20 
23 2 17 6 11 
14 9 4 21 16 
3 22 15 10 5 

1 22 17 12 7 
16 13 8 23 18 
21 2 15 6 11 
14 9 4 19 24 
3 20 25 10 5 

1 20 15 12 7 
14 25 8 21 16 
19 2 13 6 11 
24 9 4 17 22 
3 18 23 10 5 

1 18 13 24 7 
12 23 8 19 14 
17 2 11 6 25 
22 9 4 15 20 
3 16 21 10 5 

1 20 25 14 7 
10 15 8 19 24 
21 2 11 6 13 
16 9 4 23 18 
3 22 17 12 5 

1 22 11 16 7 
10 17 8 21 12 
23 2 13 6 15 
18 9 4 25 20 
3 24 19 14 5 

1 24 11 18 7 
10 19 8 23 12 
25 2 15 6 17 
20 9 4 13 22 
3 14 21 16 5 

1 16 11 20 7 
10 21 8 25 12 
15 2 17 6 19 
22 9 4 13 24 
3 14 23 18 5 

1 16 11 22 7 
10 23 8 17 12 
15 2 19 6 21 
24 9 4 13 18 
3 14 25 20 5 

1 16 11 24 7 
10 25 8 17 12 
15 2 21 6 23 
20 9 4 13 18 
3 14 19 22 5 

1 16 11 22 7 
10 21 8 17 12 
15 2 25 6 23 
20 9 4 13 18 
3 14 19 24 5 

1 16 11 22 7 
10 21 8 17 12 
15 2 23 6 25 
20 9 4 13 18 
3 14 19 24 5 

1 20 7 14 25 
10 15 24 19 8 
21 2 9 6 13 
16 11 4 23 18 
3 22 17 12 5 

1 22 7 16 25 
12 17 24 21 8 
23 2 11 6 15 
18 13 4 9 20 
3 10 19 14 5 

1 12 7 18 25 
22 17 24 13 8 
11 2 21 6 19 
16 23 4 9 14 
3 10 15 20 5 

1 12 7 18 25 
20 17 24 13 8 
11 2 19 6 23 
16 21 4 9 14 
3 10 15 22 5 

1 18 7 12 25 
8 13 24 17 20 
23 2 19 6 11 
14 9 4 21 16 
3 22 15 10 5 

1 18 7 12 25 
8 13 24 17 22 
19 2 21 6 11 
14 9 4 23 16 
3 20 15 10 5 

1 16 7 22 25 
8 21 24 17 12 
15 2 11 6 23 
20 9 4 13 18 
3 14 19 10 5 

1 14 7 20 25 
8 19 24 15 10 
13 2 9 6 21 
18 23 4 11 16 
3 12 17 22 5 

1 16 21 8 25 
22 7 24 15 20 
17 2 11 6 9 
12 23 4 19 14 
3 18 13 10 5 

1 20 13 8 25 
12 7 24 19 14 
21 2 15 6 9 
16 11 4 23 18 
3 22 17 10 5 

1 22 13 8 25 
12 7 24 21 14 
23 2 17 6 9 
18 11 4 15 20 
3 16 19 10 5 

1 14 19 8 25 
20 7 24 13 18 
15 2 9 6 23 
10 21 4 17 12 
3 16 11 22 5 

1 12 19 14 25 
18 7 24 11 20 
23 2 13 6 15 
8 17 4 21 10 
3 22 9 16 5 

1 12 17 22 25 
18 7 24 11 16 
13 2 21 6 23 
8 19 4 15 10 
3 14 9 20 5 

1 12 17 20 25 
18 7 24 11 16 
13 2 19 6 21 
8 23 4 15 10 
3 14 9 22 5 

1 12 21 16 25 
20 7 24 11 22 
13 2 15 6 17 
8 19 4 23 10 
3 14 9 18 5 

1 16 19 10 25 
18 11 24 15 20 
23 2 17 6 9 
12 7 4 21 14 
3 22 13 8 5 

1 16 21 10 25 
20 11 24 15 22 
17 2 19 6 9 
12 7 4 23 14 
3 18 13 8 5 

1 16 11 22 25 
10 21 24 17 12 
15 2 9 6 23 
20 7 4 13 18 
3 14 19 8 5 

1 20 9 14 25 
8 15 24 19 10 
21 2 11 6 13 
16 7 4 23 18 
3 22 17 12 5 

1 22 9 16 25 
8 17 24 21 10 
23 2 13 6 15 
18 7 4 11 20 
3 12 19 14 5 

1 14 9 20 25 
8 19 24 15 10 
13 2 21 6 23 
18 7 4 11 16 
3 12 17 22 5 

1 10 19 14 25 
18 13 24 9 20 
23 2 11 6 15 
12 17 4 21 8 
3 22 7 16 5 

1 10 15 22 25 
16 21 24 9 14 
11 2 19 6 23 
20 17 4 13 8 
3 12 7 18 5 

1 10 15 20 25 
16 19 24 9 14 
11 2 17 6 21 
18 23 4 13 8 
3 12 7 22 5 

1 10 21 16 25 
20 15 24 9 22 
11 2 13 6 17 
14 19 4 23 8 
3 12 7 18 5 

1 16 21 10 25 
22 9 24 15 20 
17 2 13 6 11 
8 23 4 19 14 
3 18 7 12 5 

1 20 15 10 25 
14 9 24 19 16 
21 2 17 6 11 
8 13 4 23 18 
3 22 7 12 5 

1 22 15 10 25 
14 9 24 21 16 
23 2 19 6 11 
8 13 4 17 20 
3 18 7 12 5 

1 14 19 10 25 
20 9 24 13 18 
15 2 11 6 23 
8 21 4 17 12 
3 16 7 22 5 

1 12 17 6 23 
16 7 22 11 18 
21 2 13 24 5 
8 15 4 19 10 
3 20 9 14 25 

1 12 17 6 23 
18 7 22 11 16 
13 2 19 24 5 
8 21 4 15 10 
3 14 9 20 25 

1 12 19 6 23 
18 7 22 11 20 
13 2 15 24 5 
8 17 4 21 10 
3 14 9 16 25 

1 14 19 8 23 
20 9 22 13 18 
15 2 7 24 5 
10 21 4 17 12 
3 16 11 6 25 

1 18 9 12 23 
8 13 22 17 10 
19 2 11 24 5 
14 7 4 21 16 
3 20 15 6 25 

1 20 9 14 23 
8 15 22 19 10 
21 2 13 24 5 
16 7 4 11 18 
3 12 17 6 25 

1 14 19 10 23 
20 9 22 5 18 
15 2 13 24 11 
8 21 4 17 6 
3 16 7 12 25 

1 18 15 10 23 
14 9 22 5 16 
19 2 17 24 11 
8 13 4 21 6 
3 20 7 12 25 

1 20 15 10 23 
14 9 22 5 16 
21 2 19 24 11 
8 13 4 17 6 
3 18 7 12 25 

1 12 17 10 23 
18 9 22 5 16 
13 2 11 24 21 
8 19 4 15 6 
3 14 7 20 25 

1 6 17 12 23 
16 11 22 5 18 
21 2 7 24 13 
10 15 4 19 8 
3 20 9 14 25 

1 6 11 20 23 
12 19 22 5 10 
7 2 15 24 21 
18 13 4 9 16 
3 8 17 14 25 

1 6 11 18 23 
12 17 22 5 10 
7 2 13 24 19 
16 21 4 9 14 
3 8 15 20 25 

1 6 19 14 23 
18 13 22 5 20 
7 2 9 24 15 
12 17 4 21 10 
3 8 11 16 25 

1 14 19 6 23 
20 5 22 13 18 
15 2 9 24 7 
10 21 4 17 12 
3 16 11 8 25 

1 18 11 6 23 
10 5 22 17 12 
19 2 13 24 7 
14 9 4 21 16 
3 20 15 8 25 

1 20 11 6 23 
10 5 22 19 12 
21 2 15 24 7 
16 9 4 13 18 
3 14 17 8 25 

1 12 17 6 23 
18 5 22 11 16 
13 2 7 24 21 
8 19 4 15 10 
3 14 9 20 25 

1 10 17 12 23 
16 5 22 9 18 
21 2 11 24 13 
6 15 4 19 8 
3 20 7 14 25 

1 10 15 20 23 
16 5 22 9 14 
11 2 19 24 21 
6 17 4 13 8 
3 12 7 18 25 

1 10 15 18 23 
16 5 22 9 14 
11 2 17 24 19 
6 21 4 13 8 
3 12 7 20 25 

1 10 19 14 23 
18 5 22 9 20 
11 2 13 24 15 
6 17 4 21 8 
3 12 7 16 25 

1 14 9 20 23 
10 19 22 15 8 
5 2 13 24 21 
18 11 4 7 16 
3 6 17 12 25 

1 12 9 18 23 
10 17 22 13 8 
5 2 11 24 19 
16 21 4 7 14 
3 6 15 20 25 

1 8 19 14 23 
18 13 22 9 20 
5 2 7 24 15 
12 17 4 21 10 
3 6 11 16 25 

1 6 19 12 23 
20 11 22 7 18 
5 2 15 24 13 
10 21 4 17 8 
3 16 9 14 25 

1 6 17 12 23 
16 11 22 7 18 
5 2 19 24 13 
10 15 4 21 8 
3 20 9 14 25 

1 6 17 12 23 
18 11 22 7 16 
5 2 13 24 21 
10 19 4 15 8 
3 14 9 20 25 

1 18 5 10 25 
6 11 2 19 4 
17 20 15 24 9 
12 7 22 3 14 
21 16 13 8 23 

1 16 5 10 25 
6 11 2 17 4 
15 20 13 24 9 
12 7 22 3 18 
21 14 19 8 23 

1 8 19 14 25 
18 13 2 9 4 
7 20 5 24 15 
12 17 22 3 10 
21 6 11 16 23 

1 6 9 14 25 
10 15 2 19 8 
5 20 7 24 13 
16 11 22 3 18 
21 4 17 12 23 

1 6 11 16 25 
12 17 2 7 10 
5 20 9 24 15 
18 13 22 3 8 
21 4 19 14 23 

1 6 19 12 25 
16 11 2 7 18 
5 20 17 24 13 
10 15 22 3 8 
21 4 9 14 23 

1 10 19 4 25 
18 5 2 9 14 
11 20 15 24 3 
6 17 22 13 8 
21 12 7 16 23 

1 10 17 4 25 
16 5 2 9 18 
11 20 13 24 3 
6 15 22 19 8 
21 12 7 14 23 

1 18 13 4 25 
12 5 2 19 14 
17 20 9 24 3 
6 11 22 15 8 
21 16 7 10 23 

1 16 11 4 25 
10 5 2 17 12 
15 20 7 24 3 
6 9 22 13 18 
21 14 19 8 23 

1 14 19 8 25 
6 9 2 13 18 
15 20 7 24 3 
10 5 22 17 12 
21 16 11 4 23 

1 16 7 10 25 
6 11 2 15 8 
17 20 9 24 3 
12 5 22 19 14 
21 18 13 4 23 

1 12 7 14 25 
6 15 2 19 8 
11 20 13 24 3 
16 5 22 9 18 
21 10 17 4 23 

1 12 7 16 25 
6 17 2 13 8 
11 20 15 24 3 
18 5 22 9 14 
21 10 19 4 23 

1 14 23 8 3 
22 9 2 13 18 
15 24 19 4 7 
10 21 6 17 12 
25 16 11 20 5 

1 14 21 8 3 
20 9 2 13 22 
15 24 17 4 7 
10 19 6 23 12 
25 16 11 18 5 

1 22 17 8 3 
16 9 2 23 18 
21 24 13 4 7 
10 15 6 19 12 
25 20 11 14 5 

1 20 15 8 3 
14 9 2 21 16 
19 24 11 4 7 
10 13 6 17 22 
25 18 23 12 5 

1 18 23 12 3 
10 13 2 17 22 
19 24 11 4 7 
14 9 6 21 16 
25 20 15 8 5 

1 20 11 14 3 
10 15 2 19 12 
21 24 13 4 7 
16 9 6 23 18 
25 22 17 8 5 

1 16 11 18 3 
10 19 2 23 12 
15 24 17 4 7 
20 9 6 13 22 
25 14 21 8 5 

1 16 11 20 3 
10 21 2 17 12 
15 24 19 4 7 
22 9 6 13 18 
25 14 23 8 5 

1 10 15 20 3 
16 21 2 7 14 
11 24 9 4 19 
22 17 6 13 8 
25 12 23 18 5 

1 18 23 12 3 
16 11 2 7 22 
19 24 17 4 13 
10 15 6 21 8 
25 20 9 14 5 

1 20 17 12 3 
16 11 2 7 18 
21 24 19 4 13 
10 15 6 23 8 
25 22 9 14 5 

1 8 13 18 3 
14 19 2 7 12 
9 24 21 4 17 
20 15 6 11 22 
25 10 23 16 5 

1 8 23 18 3 
22 17 2 7 12 
9 24 13 4 19 
16 21 6 11 14 
25 10 15 20 5 

1 8 21 16 3 
20 15 2 7 22 
9 24 11 4 17 
14 19 6 23 12 
25 10 13 18 5 

1 18 23 8 3 
12 7 2 17 22 
19 24 13 4 9 
14 11 6 21 16 
25 20 15 10 5 

1 20 13 8 3 
12 7 2 19 14 
21 24 15 4 9 
16 11 6 23 18 
25 22 17 10 5 

1 18 13 8 3 
12 7 2 23 14 
17 24 19 4 9 
20 11 6 15 22 
25 16 21 10 5 

1 18 13 8 3 
12 7 2 19 14 
17 24 21 4 9 
22 11 6 15 20 
25 16 23 10 5 

1 12 23 18 3 
22 7 2 11 16 
13 24 17 4 19 
8 21 6 15 10 
25 14 9 20 5 

1 12 21 16 3 
20 7 2 11 22 
13 24 15 4 17 
8 19 6 23 10 
25 14 9 18 5 

1 22 17 12 3 
16 7 2 23 18 
21 24 11 4 13 
8 15 6 19 10 
25 20 9 14 5 

1 20 15 10 3 
14 7 2 21 16 
19 24 9 4 11 
8 13 6 17 22 
25 18 23 12 5 

1 22 11 16 3 
12 17 2 23 10 
7 24 21 4 15 
18 13 6 9 20 
25 8 19 14 5 

1 20 11 16 3 
12 17 2 21 10 
7 24 19 4 15 
18 13 6 9 22 
25 8 23 14 5 

1 12 23 18 3 
22 17 2 13 10 
7 24 11 4 19 
16 21 6 9 14 
25 8 15 20 5 

1 10 21 16 3 
20 15 2 11 22 
7 24 9 4 17 
14 19 6 23 12 
25 8 13 18 5 

1 8 13 18 3 
14 19 2 23 12 
7 24 9 4 17 
20 15 6 11 22 
25 10 21 16 5 

1 8 15 20 3 
16 21 2 9 14 
7 24 11 4 19 
22 17 6 13 10 
25 12 23 18 5 

1 8 23 14 3 
18 13 2 9 22 
7 24 19 4 15 
12 17 6 21 10 
25 20 11 16 5 

1 8 19 14 3 
18 13 2 9 20 
7 24 21 4 15 
12 17 6 23 10 
25 22 11 16 5 

1 12 21 16 3 
20 15 2 11 22 
25 8 13 4 17 
14 19 6 23 10 
7 24 9 18 5 

1 12 17 22 3 
18 23 2 11 16 
13 8 25 4 21 
24 19 6 15 10 
7 14 9 20 5 

1 12 17 22 3 
18 25 2 11 16 
13 8 23 4 21 
24 19 6 15 10 
7 14 9 20 5 

1 12 17 24 3 
18 23 2 11 16 
13 8 21 4 25 
22 19 6 15 10 
7 14 9 20 5 

1 12 17 22 3 
18 21 2 11 16 
13 8 19 4 23 
20 25 6 15 10 
7 14 9 24 5 

1 12 25 20 3 
24 19 2 11 16 
13 8 17 4 21 
18 23 6 15 10 
7 14 9 22 5 

1 12 23 18 3 
22 17 2 11 24 
13 8 15 4 19 
16 21 6 25 10 
7 14 9 20 5 

1 24 19 14 3 
18 13 2 25 20 
23 8 11 4 15 
12 17 6 21 10 
7 22 9 16 5 

1 14 19 24 3 
20 25 2 13 18 
15 8 11 4 23 
10 21 6 17 12 
7 16 9 22 5 

1 18 23 12 3 
24 11 2 17 22 
19 8 15 4 13 
10 25 6 21 16 
7 20 9 14 5 

1 20 25 12 3 
16 11 2 19 24 
21 8 17 4 13 
10 15 6 23 18 
7 22 9 14 5 

1 22 17 12 3 
16 11 2 21 18 
23 8 19 4 13 
10 15 6 25 20 
7 24 9 14 5 

1 24 17 12 3 
16 11 2 23 18 
25 8 21 4 13 
10 15 6 19 22 
7 20 9 14 5 

1 22 17 12 3 
16 11 2 25 18 
21 8 23 4 13 
10 15 6 19 24 
7 20 9 14 5 

1 22 17 12 3 
16 11 2 23 18 
21 8 25 4 13 
10 15 6 19 24 
7 20 9 14 5 

1 16 21 12 3 
22 11 2 15 20 
17 8 13 4 25 
10 23 6 19 14 
7 18 9 24 5 

1 22 11 16 3 
12 17 2 21 10 
25 8 23 4 15 
18 13 6 9 20 
7 24 19 14 5 

1 22 11 16 3 
12 17 2 21 10 
23 8 25 4 15 
18 13 6 9 20 
7 24 19 14 5 

1 24 11 16 3 
12 17 2 25 10 
23 8 21 4 15 
18 13 6 9 20 
7 22 19 14 5 

1 22 11 16 3 
12 17 2 23 10 
21 8 19 4 15 
18 13 6 9 24 
7 20 25 14 5 

1 20 11 16 3 
12 25 2 21 10 
19 8 17 4 15 
24 13 6 9 22 
7 18 23 14 5 

1 18 11 24 3 
12 23 2 19 10 
17 8 15 4 25 
22 13 6 9 20 
7 16 21 14 5 

1 16 11 22 3 
12 21 2 17 10 
15 8 13 4 23 
20 25 6 9 18 
7 14 19 24 5 

1 14 25 20 3 
24 19 2 15 10 
13 8 11 4 21 
18 23 6 9 16 
7 12 17 22 5 

1 24 13 18 3 
14 19 2 23 12 
25 8 11 4 17 
20 15 6 9 22 
7 10 21 16 5 

1 12 15 20 3 
16 21 2 25 14 
11 8 13 4 19 
22 17 6 9 24 
7 10 23 18 5 

1 12 17 22 3 
18 23 2 13 16 
11 8 15 4 21 
24 19 6 9 14 
7 10 25 20 5 

1 12 19 24 3 
20 25 2 13 18 
11 8 17 4 23 
16 21 6 9 14 
7 10 15 22 5 

1 12 23 18 3 
24 17 2 13 22 
11 8 21 4 19 
16 25 6 9 14 
7 10 15 20 5 

1 12 25 18 3 
22 17 2 13 24 
11 8 23 4 19 
16 21 6 9 14 
7 10 15 20 5 

1 12 23 18 3 
22 17 2 13 24 
11 8 25 4 19 
16 21 6 9 14 
7 10 15 20 5 

1 12 21 18 3 
22 17 2 13 20 
11 8 19 4 25 
16 23 6 9 14 
7 10 15 24 5 

1 12 17 22 3 
18 23 2 9 16 
13 8 11 4 21 
24 19 6 15 10 
7 14 25 20 5 

1 14 19 24 3 
20 25 2 9 18 
15 8 13 4 23 
12 21 6 17 10 
7 16 11 22 5 

1 18 23 14 3 
24 13 2 9 22 
19 8 17 4 15 
12 25 6 21 10 
7 20 11 16 5 

1 20 25 14 3 
18 13 2 9 24 
21 8 19 4 15 
12 17 6 23 10 
7 22 11 16 5 

1 22 19 14 3 
18 13 2 9 20 
23 8 21 4 15 
12 17 6 25 10 
7 24 11 16 5 

1 24 19 14 3 
18 13 2 9 20 
25 8 23 4 15 
12 17 6 21 10 
7 22 11 16 5 

1 24 19 14 3 
18 13 2 9 20 
23 8 25 4 15 
12 17 6 21 10 
7 22 11 16 5 

1 16 21 14 3 
22 13 2 9 20 
17 8 15 4 25 
12 23 6 19 10 
7 18 11 24 5 

1 10 21 16 3 
20 15 2 9 22 
25 8 11 4 17 
14 19 6 23 12 
7 24 13 18 5 

1 10 15 20 3 
16 21 2 9 14 
11 8 25 4 19 
22 17 6 13 24 
7 12 23 18 5 

1 10 15 20 3 
16 21 2 9 14 
11 8 23 4 19 
22 17 6 13 24 
7 12 25 18 5 

1 10 15 20 3 
16 25 2 9 14 
11 8 21 4 19 
24 17 6 13 22 
7 12 23 18 5 

1 10 15 24 3 
16 23 2 9 14 
11 8 19 4 25 
22 17 6 13 20 
7 12 21 18 5 

1 10 15 22 3 
16 21 2 9 14 
11 8 17 4 23 
20 25 6 13 18 
7 12 19 24 5 

1 10 25 20 3 
24 19 2 9 14 
11 8 15 4 21 
18 23 6 13 16 
7 12 17 22 5 

1 10 23 18 3 
22 17 2 9 24 
11 8 13 4 19 
16 21 6 25 14 
7 12 15 20 5 

1 22 9 16 3 
12 17 2 21 10 
23 8 11 4 15 
18 13 6 25 20 
7 24 19 14 5 

1 24 9 18 3 
14 19 2 23 10 
25 8 13 4 17 
20 15 6 11 22 
7 12 21 16 5 

1 14 9 20 3 
16 21 2 25 10 
13 8 15 4 19 
22 17 6 11 24 
7 12 23 18 5 

1 14 9 22 3 
18 23 2 15 10 
13 8 17 4 21 
24 19 6 11 16 
7 12 25 20 5 

1 14 9 24 3 
20 25 2 15 10 
13 8 19 4 23 
18 21 6 11 16 
7 12 17 22 5 

1 14 9 20 3 
24 19 2 15 10 
13 8 23 4 21 
18 25 6 11 16 
7 12 17 22 5 

1 14 9 20 3 
24 19 2 15 10 
13 8 25 4 21 
18 23 6 11 16 
7 12 17 22 5 

1 14 9 20 3 
22 19 2 15 10 
13 8 21 4 25 
18 23 6 11 16 
7 12 17 24 5 

1 20 9 14 3 
10 15 2 19 22 
25 8 21 4 13 
16 11 6 23 18 
7 24 17 12 5 

1 20 9 14 3 
10 15 2 19 24 
21 8 25 4 13 
16 11 6 23 18 
7 22 17 12 5 

1 20 9 14 3 
10 15 2 19 24 
21 8 23 4 13 
16 11 6 25 18 
7 22 17 12 5 

1 24 9 14 3 
10 15 2 25 20 
23 8 19 4 13 
16 11 6 21 18 
7 22 17 12 5 

1 22 9 14 3 
10 15 2 23 18 
21 8 17 4 13 
16 11 6 19 24 
7 20 25 12 5 

1 20 9 14 3 
10 25 2 21 16 
19 8 15 4 13 
24 11 6 17 22 
7 18 23 12 5 

1 18 9 24 3 
10 23 2 19 14 
17 8 13 4 25 
22 11 6 15 20 
7 16 21 12 5 

1 16 9 22 3 
10 21 2 17 12 
15 8 11 4 23 
20 25 6 13 18 
7 14 19 24 5 

1 18 5 12 3 
8 13 2 17 6 
19 22 7 4 11 
14 9 24 21 16 
23 20 15 10 25 

1 10 5 16 3 
12 17 2 21 6 
9 22 11 4 15 
18 13 24 7 20 
23 8 19 14 25 

1 10 5 18 3 
14 19 2 11 6 
9 22 13 4 17 
20 15 24 7 12 
23 8 21 16 25 

1 16 5 10 3 
6 11 2 15 20 
17 22 19 4 9 
12 7 24 21 14 
23 18 13 8 25 

1 20 5 10 3 
6 11 2 21 16 
19 22 15 4 9 
12 7 24 17 14 
23 18 13 8 25 

1 18 5 10 3 
6 11 2 19 14 
17 22 13 4 9 
12 7 24 15 20 
23 16 21 8 25 

1 16 21 6 3 
10 5 2 15 20 
17 22 11 4 7 
12 9 24 19 14 
23 18 13 8 25 

1 18 11 6 3 
10 5 2 17 12 
19 22 13 4 7 
14 9 24 21 16 
23 20 15 8 25 

1 16 11 6 3 
10 5 2 21 12 
15 22 17 4 7 
18 9 24 13 20 
23 14 19 8 25 

1 16 11 6 3 
10 5 2 17 12 
15 22 19 4 7 
20 9 24 13 18 
23 14 21 8 25 

1 10 21 16 3 
20 5 2 9 14 
11 22 15 4 17 
6 19 24 13 8 
23 12 7 18 25 

1 10 19 14 3 
18 5 2 9 20 
11 22 13 4 15 
6 17 24 21 8 
23 12 7 16 25 

1 20 15 10 3 
14 5 2 21 16 
19 22 9 4 11 
6 13 24 17 8 
23 18 7 12 25 

1 18 13 8 3 
12 5 2 19 14 
17 22 7 4 9 
6 11 24 15 20 
23 16 21 10 25 

1 14 21 8 3 
20 9 2 13 18 
15 22 19 4 7 
10 5 24 17 12 
23 16 11 6 25 

1 14 19 8 3 
18 9 2 13 20 
15 22 17 4 7 
10 5 24 21 12 
23 16 11 6 25 

1 20 15 8 3 
14 9 2 21 16 
19 22 13 4 7 
10 5 24 17 12 
23 18 11 6 25 

1 18 13 8 3 
12 9 2 19 14 
17 22 11 4 7 
10 5 24 15 20 
23 16 21 6 25 

1 16 21 10 3 
6 11 2 15 20 
17 22 7 4 9 
12 5 24 19 14 
23 18 13 8 25 

1 18 7 12 3 
6 13 2 17 8 
19 22 9 4 11 
14 5 24 21 16 
23 20 15 10 25 

1 12 7 16 3 
6 17 2 21 8 
11 22 13 4 15 
18 5 24 9 20 
23 10 19 14 25 

1 12 7 18 3 
6 19 2 13 8 
11 22 15 4 17 
20 5 24 9 14 
23 10 21 16 25 

1 8 21 16 3 
20 15 2 7 12 
9 22 13 4 17 
14 19 24 11 6 
23 10 5 18 25 

1 8 19 14 3 
18 13 2 7 20 
9 22 11 4 15 
12 17 24 21 6 
23 10 5 16 25 

1 20 15 10 3 
14 9 2 21 16 
19 22 7 4 11 
8 13 24 17 6 
23 18 5 12 25 

1 16 21 8 3 
12 7 2 15 20 
17 22 13 4 9 
6 11 24 19 14 
23 18 5 10 25 

1 18 13 8 3 
12 7 2 17 14 
19 22 15 4 9 
6 11 24 21 16 
23 20 5 10 25 

1 18 13 8 3 
12 7 2 21 14 
17 22 19 4 9 
6 11 24 15 20 
23 16 5 10 25 

1 18 7 12 25 
8 13 2 19 6 
3 20 17 24 11 
14 9 22 5 16 
21 4 15 10 23 

1 16 7 12 25 
8 13 2 17 6 
3 20 15 24 11 
14 9 22 5 18 
21 4 19 10 23 

1 8 19 14 25 
18 13 2 9 6 
3 20 7 24 15 
12 17 22 5 10 
21 4 11 16 23 

1 6 17 12 25 
16 11 2 7 18 
3 20 5 24 13 
10 15 22 19 8 
21 4 9 14 23 

1 4 9 14 25 
10 15 2 19 8 
3 20 5 24 13 
16 11 22 7 18 
21 6 17 12 23 

1 4 11 16 25 
12 17 2 5 10 
3 20 7 24 15 
18 13 22 9 6 
21 8 19 14 23 

1 4 19 10 25 
14 9 2 5 18 
3 20 15 24 11 
8 13 22 17 6 
21 16 7 12 23 

1 4 15 10 25 
14 9 2 5 16 
3 20 17 24 11 
8 13 22 19 6 
21 18 7 12 23 

1 12 19 6 25 
18 7 2 11 16 
13 20 17 24 5 
8 3 22 15 10 
21 14 9 4 23 

1 12 17 6 25 
16 7 2 11 18 
13 20 15 24 5 
8 3 22 19 10 
21 14 9 4 23 

1 18 13 6 25 
12 7 2 19 14 
17 20 11 24 5 
8 3 22 15 10 
21 16 9 4 23 

1 16 11 6 25 
10 7 2 17 12 
15 20 9 24 5 
8 3 22 13 18 
21 14 19 4 23 

1 14 19 8 25 
4 9 2 13 18 
15 20 5 24 7 
10 3 22 17 12 
21 16 11 6 23 

1 16 5 10 25 
4 11 2 15 6 
17 20 7 24 9 
12 3 22 19 14 
21 18 13 8 23 

1 10 5 14 25 
4 15 2 19 6 
9 20 11 24 13 
16 3 22 7 18 
21 8 17 12 23 

1 10 5 16 25 
4 17 2 11 6 
9 20 13 24 15 
18 3 22 7 12 
21 8 19 14 23 

Time Complexity: O(8N*N)
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :