Given two integers N and M denoting the dimensions of a chessboard, and two integers X and Y denoting the King’s position, i.e. the cell (X, Y). The task is to find the number of cells the Queen can visit that are not visited by the King if it gets replaced by the King. The King visits all his adjacent cells and the Queen can move diagonally, horizontally, and vertically.
Input: N = 8, M = 8, X = 1, Y = 1
Below is the image to show the movement of King and Queen:
Suppose K represents King and * represents cells visited by the king and Q represents Queen and # represent the cells visited by the queen.
So, the total squares can be visited by the queen is 18.
Input: N = 2, M = 1, X = 1, Y = 1
Approach: The idea is to first find the total number of positions that the Queen can visit. Then find the total number of positions that the King can visit. The number of cells that can only be visited by the Queen will be the number of cells the King can visit subtracted from the number of cells the Queen can visit. Follow the below steps to solve the problem:
- Initialize queenMoves by 0 and calculate the total moves of the Queen as follows:
If N – X > 0 and M – Y > 0, queenMoves = queenMoves + min(N – X, M – Y)
If X – 1 > 0 and Y – 1 > 0, queenMoves = queenMoves + min(Y – 1, X – 1)
If X – 1 > 0 and M – Y > 0, queenMoves = queenMoves + min(X – 1, M – Y)
If N – X > 0 and Y – 1 > 0, queenMoves = queenMoves + min(N – X, Y – 1)
At last, add the answer for horizontal and vertical movements as queenMoves = queenMoves + (N – 1) + (M – 1)
- Initialize kingMoves as 0 and calculate King’s moves as follows:
If X + 1 <= N, kingMoves = kingMoves + 1
If X – 1 > 0, kingMoves = kingMoves + 1
If Y + 1 <= M, kingMoves = kingMoves + 1
If Y – 1 > 0, kingMoves = kingMoves + 1
If X + 1 <= N and Y + 1 <= M, kingMoves = kingMoves + 1
If X + 1 <= N and Y – 1 > 0, kingMoves = kingMoves + 1
If X – 1 > 0 and Y – 1 > 0, kingMoves = kingMoves + 1
If X – 1 > 0 and Y + 1 <= M, kingMoves = kingMoves + 1
- Print the absolute difference between the Queen’s and King’s calculated in the above steps as the result.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)
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.
- Total position where king can reach on a chessboard in exactly M moves
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Check if any King is unsafe on the Chessboard or not
- Check if a Queen can attack a given cell on chessboard
- Find all the queens attacking the king in a chessboard
- Minimum Cuts can be made in the Chessboard such that it is not divided into 2 parts
- Count the total number of squares that can be visited by Bishop in one move
- Number of cells a queen can move with obstacles on the chessborad
- Check if a cell can be visited more than once in a String
- Minimum number of moves required to reach the destination by the king in a chess board
- Maximum bishops that can be placed on N*N chessboard
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Maximum non-attacking Knights that can be placed on an N*M 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
- Check if the given chessboard is valid or not
- Count distinct points visited on the number line
- 8 queen problem
- N Queen Problem | Backtracking-3
- Printing all solutions in N-Queen Problem
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.
Improved By : akhilsaini