Place K-knights such that they do not attack each other

Given integers M, N and K, the task is to place K knights on an M*N chessboard such that they don’t attack each other.
The knights are expected to be placed on different squares on the board. A knight can move two squares vertically and one square horizontally or two squares horizontally and one square vertically. The knights attack each other if one of them can reach the other in single move. There are multiple ways of placing K knights on an M*N board or sometimes, no way of placing them. We are expected to list out all the possible solutions.

Examples:

Input: M = 3, N = 3, K = 5
Output:
K A K
A K A
K A K

A K A
K K K
A K A

Total number of solutions : 2

Input: M = 5, N = 5, K = 13
Output:
K A K A K
A K A K A
K A K A K
A K A K A
K A K A K

Total number of solutions : 1

Approach: This problem can be solved using backtracking.
The idea is to place the knights one by one starting from first row and first column and moving forward to first row and second column such that they don’t attack each other. When one row gets over, we move to the next row. Before placing a knight, we always check if the block is safe i.e. it is not an attacking position of some other knight. If it is safe, we place the knight and mark it’s attacking position on the board else we move forward and check for other blocks. While following this procedure, we make a new board every time we insert a new knight into our board. This is done because if we get one solution and we need other solutions, then we can backtrack to our old board with old configuration of knights which can then be checked for other possible solutions. The process of backtracking is continued till we get all our possible solutions.

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <iostream>
using namespace std;
  
/* m*n is the board dimension
k is the number of knights to be placed on board 
count is the number of possible solutions */
int m, n, k;
int count = 0;
  
/* This function is used to create an empty m*n board */
void makeBoard(char** board)
{
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            board[i][j] = '_';
        }
    }
}
  
/* This function displays our board */
void displayBoard(char** board)
{
    cout << endl
         << endl;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
            cout << " " << board[i][j] << " ";
        }
        cout << endl;
    }
}
  
/* This function marks all the attacking
 position of a knight placed at board[i][j]
 position */
void attack(int i, int j, char a,
            char** board)
{
  
    /* conditions to ensure that the 
       block to be checked is inside the board */
    if ((i + 2) < m && (j - 1) >= 0) {
        board[i + 2][j - 1] = a;
    }
    if ((i - 2) >= 0 && (j - 1) >= 0) {
        board[i - 2][j - 1] = a;
    }
    if ((i + 2) < m && (j + 1) < n) {
        board[i + 2][j + 1] = a;
    }
    if ((i - 2) >= 0 && (j + 1) < n) {
        board[i - 2][j + 1] = a;
    }
    if ((i + 1) < m && (j + 2) < n) {
        board[i + 1][j + 2] = a;
    }
    if ((i - 1) >= 0 && (j + 2) < n) {
        board[i - 1][j + 2] = a;
    }
    if ((i + 1) < m && (j - 2) >= 0) {
        board[i + 1][j - 2] = a;
    }
    if ((i - 1) >= 0 && (j - 2) >= 0) {
        board[i - 1][j - 2] = a;
    }
}
  
/* If the position is empty,
   place the knight */
bool canPlace(int i, int j, char** board)
{
    if (board[i][j] == '_')
        return true;
    else
        return false;
}
  
/* Place the knight at [i][j] position
   on board */
void place(int i, int j, char k, char a,
           char** board, char** new_board)
{
  
    /* Copy the configurations of
     old board to new board */
    for (int y = 0; y < m; y++) {
        for (int z = 0; z < n; z++) {
            new_board[y][z] = board[y][z];
        }
    }
  
    /* Place the knight at [i][j]
    position on new board */
    new_board[i][j] = k;
  
    /* Mark all the attacking positions
    of newly placed knight on the new board */
    attack(i, j, a, new_board);
}
  
/* Function for placing knights on board
   such that they don't attack each other */
void kkn(int k, int sti, int stj, char** board)
{
  
    /* If there are no knights left to be placed,
     display the board and increment the count */
    if (k == 0) {
        displayBoard(board);
        count++;
    }
    else {
  
        /* Loop for checking all the 
       positions on m*n board */
        for (int i = sti; i < m; i++) {
            for (int j = stj; j < n; j++) {
  
                /* Is it possible to place knight at 
           [i][j] position on board? */
                if (canPlace(i, j, board)) {
  
                    /* Create a a new board and place the 
                      new knight on it */
                    char** new_board = new char*[m];
                    for (int x = 0; x < m; x++) {
                        new_board[x] = new char[n];
                    }
                    place(i, j, 'K', 'A', board, new_board);
  
                    /* Call the function recursively for
                      (k-1) leftover knights */
                    kkn(k - 1, i, j, new_board);
  
                    /* Delete the new board 
                    to free up the memory */
                    for (int x = 0; x < m; x++) {
                        delete[] new_board[x];
                    }
                    delete[] new_board;
                }
            }
            stj = 0;
        }
    }
}
  
// Driver code
int main()
{
    m = 4, n = 3, k = 6;
  
    /* Creation of a m*n board */
    char** board = new char*[m];
    for (int i = 0; i < m; i++) {
        board[i] = new char[n];
    }
  
    /* Make all the places are empty */
    makeBoard(board);
  
    kkn(k, 0, 0, board);
  
    cout << endl
         << "Total number of solutions : "
         << count;
    return 0;
}

chevron_right


Output:

 K  K  K 
 A  A  A 
 A  A  A 
 K  K  K 

 K  A  K 
 A  K  A 
 K  A  K 
 A  K  A 

 A  K  A 
 K  A  K 
 A  K  A 
 K  A  K 

Total number of solutions : 3


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.