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.
Input: N = 1, M = 4
Place a knight on every cell of the chessboard.
Input: N = 4, M = 5
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
- 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.
- Maximum bishops that can be placed on N*N chessboard
- Maximum non-attacking Rooks that can be placed on an N*N Chessboard
- Find position of non-attacking Rooks in lexicographic order that can be placed on N*N chessboard
- Place K-knights such that they do not attack each other
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Total position where king can reach on a chessboard in exactly M moves
- Check if a Queen can attack a given cell on chessboard
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Count positions in a chessboard that can be visited by the Queen which are not visited by the King
- Probability of Knight to remain in the chessboard
- Check if the given chessboard is valid or not
- Chessboard Pawn-Pawn game
- Count squares with odd side length in Chessboard
- Puzzle | Minimum colors required to color a chessboard with no two given pair threatening each other
- Check if any King is unsafe on the Chessboard or not
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum array sum that can be obtained after exactly k changes
- Maximum number of nodes which can be reached from each node in a graph.
- Maximum items that can be bought with the given type of coins
- Maximum number of strings that can be formed with given zeros and ones
Below is the implementation of the above approach:
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.
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.