Description
Given an n x n chessboard with a valid knight's tour marked 0 to n²-1, verify if it represents a valid knight tour. Return true or false.
Examples
Input:
grid = [[0,11,16,5,20],[17,4,19,10,15],[12,1,8,21,6],[3,18,23,14,9],[24,13,2,7,22]]Output:
trueExplanation:
A knight tour is valid if the knight visits every cell exactly once following L-shaped moves (2 squares in one direction, 1 square perpendicular). The algorithm locates each position 0 through n^2-1 in sequence and verifies that consecutive positions differ by exactly one valid knight move: |dx|=1 and |dy|=2, or |dx|=2 and |dy|=1.
Input:
grid = [[0]]Output:
trueExplanation:
A 1x1 board with only position 0 is trivially a valid knight tour since the knight starts and ends at the same (only) cell.
Input:
grid = [[8,1,6],[3,5,7],[4,9,2]]Output:
falseExplanation:
The grid does not represent a valid knight tour because consecutive positions in the sequence are not all reachable by valid L-shaped knight moves.
Constraints
- •
n == grid.length