Maximum non-attacking Knights that can be placed on an N*M Chessboard
Last Updated :
15 Nov, 2023
Given an
N*M
chessboard. The task is to find the maximum number of knights that can be placed on the given chessboard such that no knight attack some other knight.
Example
Input: N = 1, M = 4 Output: 4 Place a knight on every cell of the chessboard. Input: N = 4, M = 5 Output: 10
Approach:
As we know that a knight can attack in two ways. Here are the places which he can attack.
Here, in the picture, the knight is on white color and attacks only the black color. Thus. we concluded that a knight can attack only on a different color. We can take help of this fact and use it for our purpose. Now as we know knight attacks on different color so we can keep all knights on the same color i.e. all on white or all on black. Thus making the highest number of knights which can be placed. To find the number of black or white, it is simply half of the total blocks on board.
Total Blocks = n * m Blocks of the same color = (n * m) / 2
Corner cases:
- If there is only a single row or column. Then all the blocks can be filled by knights as a knight cannot attack in the same row or column.
- If there are only two rows or columns. Then every two columns (or rows) will be filled with knights and every consecutive two columns (or rows) will remain empty. As demonstrated in the picture.
-
- Below is the implementation of the above approach:
-
C++
#include <bits/stdc++.h>
using namespace std;
int max_knight( int n, int m)
{
if (m == 1 || n == 1) {
return max(m, n);
}
else if (m == 2 || n == 2) {
int c = 0;
c = (max(m, n) / 4) * 4;
if (max(m, n) % 4 == 1) {
c += 2;
}
else if (max(m, n) % 4 > 1) {
c += 4;
}
return c;
}
else {
return (((m * n) + 1) / 2);
}
}
int main()
{
int n = 4, m = 5;
cout << max_knight(n, m);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int max_knight( int n, int m)
{
if (m == 1 || n == 1 )
{
return Math.max(m, n);
}
else if (m == 2 || n == 2 )
{
int c = 0 ;
c = (Math.max(m, n) / 4 ) * 4 ;
if (Math.max(m, n) % 4 == 1 )
{
c += 2 ;
}
else if (Math.max(m, n) % 4 > 1 )
{
c += 4 ;
}
return c;
}
else
{
return (((m * n) + 1 ) / 2 );
}
}
public static void main (String[] args)
{
int n = 4 , m = 5 ;
System.out.println (max_knight(n, m));
}
}
|
Python3
def max_knight(n, m) :
if (m = = 1 or n = = 1 ) :
return max (m, n);
elif (m = = 2 or n = = 2 ) :
c = 0 ;
c = ( max (m, n) / / 4 ) * 4 ;
if ( max (m, n) % 4 = = 1 ) :
c + = 2 ;
elif ( max (m, n) % 4 > 1 ) :
c + = 4 ;
return c;
else :
return (((m * n) + 1 ) / / 2 );
if __name__ = = "__main__" :
n = 4 ; m = 5 ;
print (max_knight(n, m));
|
C#
using System;
class GFG
{
static int max_knight( int n, int m)
{
if (m == 1 || n == 1)
{
return Math.Max(m, n);
}
else if (m == 2 || n == 2)
{
int c = 0;
c = (Math.Max(m, n) / 4) * 4;
if (Math.Max(m, n) % 4 == 1)
{
c += 2;
}
else if (Math.Max(m, n) % 4 > 1)
{
c += 4;
}
return c;
}
else
{
return (((m * n) + 1) / 2);
}
}
static public void Main ()
{
int n = 4, m = 5;
Console.Write(max_knight(n, m));
}
}
|
Javascript
function maxKnight(n, m) {
if (m === 1 || n === 1) {
return Math.max(m, n);
}
else if (m === 2 || n === 2) {
let c = 0;
c = (Math.max(m, n) / 4) * 4;
if (Math.max(m, n) % 4 === 1) {
c += 2;
} else if (Math.max(m, n) % 4 > 1) {
c += 4;
}
return c;
}
else {
return Math.floor((m * n + 1) / 2);
}
}
let n = 4, m = 5;
console.log(maxKnight(n, m));
|
-
Share your thoughts in the comments
Please Login to comment...