Count all possible visited cells of a knight after N moves
Given the current position of a Knight as (i, j), find the count of different possible positions visited by a knight after N moves (in a 10 x 10 board).
Input: i = 3, j = 3, n = 1
The Knight is initially at position . After one move it can visit 8 more cells
Input: i = 3, j = 3, n = 2
Approach: The idea is simple, we start from a given position, try all possible moves. After every move, recursively call for n-1 moves. We need to ensure that we never visit a cell again. We make a visited boolean matrix which will serve as a visited matrix so that the positions do not get repeated. When we visit a position, mark that position as true in the matrix.
- Take a boolean visited matrix (10X10) and initialize all the cells as false (non-visited)
- Create two vectors with all possible moves of a knight. We find that there are 8 possible moves of a knight.
- Valid position = The knight is inside the boundaries of the board and the cell is non-visited.
- Call the method for the next valid position with n = n-1.
- If n == 0, return.
Below is the implementation of the above approach: