Given integers i, j, k and n where (i, j) is the initial position of the Knight on a n * n chessboard, the task is to find the number of positions the Knight can move to in exactly k moves.
Input: i = 5, j = 5, k = 1, n = 10
Input: i = 0, j = 0, k = 2, n = 10
The knight can see total 10 different positions in 2nd move.
Approach: Use a recursive approach to solve the problem.
First find all the possible positions where the knight can move to so if the initial position is i, j. Get to all valid locations in single move and recursively find all the possible positions where knight can move to in k – 1 steps from there. The base case of this recursion is when k == 0 (no move to make) then we will mark the position of the chessboard as visited if it is unmarked and increase the count. Finally, display the count .
Below is the implementation of the above approach:
- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- 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
- Probability of Knight to remain in the chessboard
- Possible moves of knight
- Puzzle | Can a Knight reach bottom from top by visiting all squares
- Count all possible position that can be reached by Modified Knight
- Maximum bishops that can be placed on N*N chessboard
- Check if a Queen can attack a given cell on chessboard
- 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
- Count positions in a chessboard that can be visited by the Queen which are not visited by the King
- The Knight's tour problem | Backtracking-1
- Minimum steps to reach target by a Knight | Set 1
- Warnsdorff's algorithm for Knight’s tour problem
- Minimum steps to reach target by a Knight | Set 2
- Count of all possible ways to reach a target by a Knight
- Number of cells a queen can move with obstacles on the chessborad
- Count the total number of squares that can be visited by Bishop in one move
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 : rituraj_jain