222k views
1 vote
6 Knightmare Give an algorithm to find the number of ways you can place knights on an N by M (M < N) chessboard such that no two knights can attack each other (there can be any number of knights on the board, including zero knights). Clearly describe your algorithm and prove its correctness.

User SergV
by
7.0k points

1 Answer

3 votes

Answer:

Function to return the maximum number of knights that can be placed on the given chessboard such that no two knights attack each other.

Explanation:

In this algorithm it's fully described the maximum number

#include <bits/stdc++.h>

using namespace std;

int max_knight(int n, int m)

{

// Check for corner case #1

//If row or column is 1

if (m == 1 || n == 1) {

// If yes, then simply print total blocks

// which will be the max of row or column

return max(m, n);

}

// Check for corner case #2

// If row or column is 2

else if (m == 2 || n == 2) {

// If yes, then simply calculate

// consecutive 2 rows or columns

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;

}

// For general case, just print the

// half of total blocks

else {

return (((m * n) + 1) / 2);

}

}

// Driver code

int main()

{

int n = 4, m = 5;

cout << max_knight(n, m);

return 0;

}

User DadaB
by
7.7k points