Problem
A knight’s tour is a sequence of moves by a knight on a chessboard such that all squares are visited once.
Given N, write a function to return the number of knight’s tours on an N by N chessboard.
We have already see Knight’s Tour
Examples
Example 1:
| |
Solution
Method 1 - Backtracking
Code
| |
Complexity
- ⏰ Time complexity:
O(8 ^ (N^2))- Each position on the board can make up to 8 possible knight moves.
- In the worst case, the depth of the recursion can be (
N^2) since the knight needs to visit every cell on anN^2 - For each of the ( N^2 ) cells, you can have up to 8 possible moves. This gives a high upper bound on the number of recursive calls.
- 🧺 Space complexity:
O(N*N)- The board itself is an
(N*N)matrix - Recursion stack can go as deep as
N^2
- The board itself is an