Given the position of the king on an 8 X 8 chessboard, the task is to count the total number of squares that can be visited by the king in m moves. The position of the king is denoted using row and column number.
Note: The square which is currently acquired by the king is already visited and will be counted in the result.
Input: r = 4, c = 4, m = 1
Input: r = 4, c = 4, m = 2
Approach: A king can move one square in any direction (i.e horizontally, vertically and diagonally). So, in one move king can visit its adjacent squares.
So, A square which is within m units distance (Considering 1 Square as 1 unit distance) from the king’s current position can be visited in m moves.
- For all squares of the chessboard, check if a particular square is at m unit distance away or less from King’s current position.
- Increment count, if step 1 is true.
- Print the count
Below is the implementation of the above approach:
- Total position where king can reach on a chessboard in exactly M moves
- Minimum number of moves required to reach the destination by the king in a chess board
- Number of blocks in a chessboard a knight can move to in exactly k moves
- Minimum number of moves to reach N starting from (1, 1)
- Minimum moves to reach target on a infinite line | Set 2
- Find minimum moves to reach target on an infinite line
- Minimum time to reach a point with +t and -t moves at time t
- Total ways of choosing X men and Y women from a total of M men and W women
- Maximum bishops that can be placed on N*N chessboard
- Check if the given chessboard is valid or not
- Probability of Knight to remain in the chessboard
- Count Distinct Rectangles in N*N Chessboard
- Maximum non-attacking Knights that can be placed on an N*M Chessboard
- Program to find number of squares in a chessboard
- Check if a Queen can attack a given cell on chessboard
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 : AnkitRai01