# Number of blocks in a chessboard a knight can move to in exactly k moves

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.

**Examples:**

Input:i = 5, j = 5, k = 1, n = 10

Output:8

Input:i = 0, j = 0, k = 2, n = 10

Output: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:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function that will be called recursively ` `int` `recursive_solve(` `int` `i, ` `int` `j, ` `int` `steps, ` `int` `n, ` ` ` `map<pair<` `int` `, ` `int` `>, ` `int` `> &m) ` `{ ` ` ` `// If there's no more move to make and ` ` ` `// this position hasn't been visited before ` ` ` `if` `(steps == 0 && m[make_pair(i, j)] == 0) { ` ` ` ` ` `// mark the position ` ` ` `m[make_pair(i, j)] = 1; ` ` ` ` ` `// increase the count ` ` ` `return` `1; ` ` ` `} ` ` ` ` ` `int` `res = 0; ` ` ` `if` `(steps > 0) { ` ` ` ` ` `// valid movements for the knight ` ` ` `int` `dx[] = { -2, -1, 1, 2, -2, -1, 1, 2 }; ` ` ` `int` `dy[] = { -1, -2, -2, -1, 1, 2, 2, 1 }; ` ` ` ` ` `// find all the possible positions ` ` ` `// where knight can move from i, j ` ` ` `for` `(` `int` `k = 0; k < 8; k++) { ` ` ` ` ` `// if the positions lies within the ` ` ` `// chessboard ` ` ` `if` `((dx[k] + i) >= 0 ` ` ` `&& (dx[k] + i) <= n - 1 ` ` ` `&& (dy[k] + j) >= 0 ` ` ` `&& (dy[k] + j) <= n - 1) { ` ` ` ` ` `// call the function with k-1 moves left ` ` ` `res += recursive_solve(dx[k] + i, dy[k] + j, ` ` ` `steps - 1, n, m); ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// find all the positions where the knight can ` `// move after k steps ` `int` `solve(` `int` `i, ` `int` `j, ` `int` `steps, ` `int` `n) ` `{ ` ` ` `map<pair<` `int` `, ` `int` `>, ` `int` `> m; ` ` ` `return` `recursive_solve(i, j, steps, n, m); ` `} ` ` ` `// driver code ` `int` `main() ` `{ ` ` ` `int` `i = 0, j = 0, k = 2, n = 10; ` ` ` ` ` `cout << solve(i, j, k, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

# Python3 implementation of above approach

from collections import defaultdict

# Function that will be called recursively

def recursive_solve(i, j, steps, n, m):

# If there’s no more move to make and

# this position hasn’t been visited before

if steps == 0 and m[(i, j)] == 0:

# mark the position

m[(i, j)] = 1

# increase the count

return 1

res = 0

if steps > 0:

# valid movements for the knight

dx = [-2, -1, 1, 2, -2, -1, 1, 2]

dy = [-1, -2, -2, -1, 1, 2, 2, 1]

# find all the possible positions

# where knight can move from i, j

for k in range(0, 8):

# If the positions lies

# within the chessboard

if (dx[k] + i >= 0 and

dx[k] + i <= n - 1 and
dy[k] + j >= 0 and

dy[k] + j <= n - 1):
# call the function with k-1 moves left
res += recursive_solve(dx[k] + i, dy[k] + j,
steps - 1, n, m)
return res
# Find all the positions where the
# knight can move after k steps
def solve(i, j, steps, n):
m = defaultdict(lambda:0)
return recursive_solve(i, j, steps, n, m)
# Driver code
if __name__ == "__main__":
i, j, k, n = 0, 0, 2, 10
print(solve(i, j, k, n))
# This code is contributed by Rituraj Jain
[tabbyending]

**Output:**

10

## Recommended Posts:

- Check if a king can move a valid move or not when N nights are there in a modified chessboard
- Probability of Knight to remain in the chessboard
- Possible moves of knight
- 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
- Count number of 1s in the array after N moves
- Minimum number of given moves required to make N divisible by 25
- 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
- Minimum number of moves required to reach the destination by the king in a chess board
- Find the minimum number of preprocess moves required to make two strings equal
- The Knight's tour problem | Backtracking-1
- Minimum steps to reach target by a Knight | Set 2
- Count all possible position that can be reached by Modified Knight
- Warnsdorff's algorithm for Knight’s tour 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 contribute@geeksforgeeks.org. 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.