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
- Place K-knights such that they do not attack each other
- Check if the given chessboard is valid or not
- Probability of Knight to remain in the chessboard
- Check if a Queen can attack a given cell on chessboard
- Count squares with odd side length in Chessboard
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- 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
- Chessboard Pawn-Pawn game
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum element in an array such that its previous and next element product is maximum
- Maximum value of arr[i] % arr[j] for a given array
- Find the node whose xor with x gives maximum value
- Longest sub-sequence with maximum GCD
Below is the implementation of the above approach:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.