Check Knight Tour Configuration

There is a knight on an n x n chessboard. In a valid configuration, the knight starts at the top-left cell of the board and visits every cell on the board exactly once.

You are given an n x n integer matrix grid consisting of distinct integers from the range [0, n * n - 1] where grid[row][col] indicates that the cell (row, col) is the grid[row][col]^th cell that the knight visited. The moves are 0-indexed.

Return true if grid represents a valid configuration of the knight's movements or false otherwise.

Note that a valid knight move consists of moving two squares vertically and one square horizontally, or two squares horizontally and one square vertically.

Example 1
 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
Inputgrid = [[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]]
Outputtrue
The above diagram represents the grid, and it can be shown that it is a valid configuration.
Example 2
0 3 6
5 8 1
2 7 4
Inputgrid = [[0,3,6],[5,8,1],[2,7,4]]
Outputfalse
The above diagram represents the grid, and the 8^th move of the knight is not valid considering its position after the 7^th move.

Constraints

  • n == grid.length == grid[i].length
  • 3 <= n <= 7
  • 0 <= grid[row][col] < n * n
  • All integers in grid are unique.

Asked at 4 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate