Consider a N X N chessboard with a Queen and K obstacles. The Queen cannot pass through obstacles. Given the position (x, y) of Queen, the task is to find the number of cells the queen can move.
Input : N = 8, x = 4, y = 4, K = 0 Output : 27 Input : N = 8, x = 4, y = 4, K = 1, kx1 = 3, ky1 = 5 Output : 24
The idea is to iterate over the cells the queen can attack and stop until there is an obstacle or end of the board. To do that, we need to iterate horizontally, vertically and diagonally. The moves from position (x, y) can be:
(x+1, y): one step horizontal move to the right.
(x-1, y): one step horizontal move to the left.
(x+1, y+1): one step diagonal move up-right.
(x-1, y-1): one step diagonal move down-left.
(x-1, y+1): one step diagonal move left-up.
(x+1, y-1): one step diagonal move right-down.
(x, y+1): one step downward.
(x, y-1): one step upward.
Below is C++ implementation of this approach:
The idea is to iterate over the obstacles and for those who are in the queen’s path, we calculate the free cells upto that obstacle. If there is no obstacle in the path we have to calculate the number of free cells upto end of board in that direction.
For any (x1, y1) and (x2, y2):
- If they are horizontally at same level: abs(x1 – x2 – 1)
- If they are vertically at same level: abs(y1 – y2 – 1) is the number of free cells between.
- If they are diagonal: both abs(x1 – x2 – 1) or abs(y1 – y2 – 1) is the number of free cells between.
Below is the implementation of this approach:
This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Unique paths in a Grid with Obstacles
- Check if a Queen can attack a given cell on chessboard
- 8 queen problem
- N Queen Problem | Backtracking-3
- N Queen Problem using Branch And Bound
- Printing all solutions in N-Queen Problem
- N Queen in O(n) space
- N-Queen Problem | Local Search using Hill climbing with random neighbour
- Check if cells numbered 1 to K in a grid can be connected after removal of atmost one blocked cell
- Check if a given string can be formed using characters of adjacent cells of a Matrix
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Count the total number of squares that can be visited by Bishop in one move
- Number of cells in the right and left diagonals passing through (x, y) in a matrix
- Find the number of cells in the table contains X
- Find Number of Even cells in a Zero Matrix after Q queries
- Number of cells in a matrix that satisfy the given condition