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 trueifgridrepresents a valid configuration of the knight ’s movements orfalseotherwise.
Note that a valid knight move consists of moving two squares vertically and one square horizontally, or two squares horizontally and one square vertically. The figure below illustrates all the possible eight moves of a knight from some cell.
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: The above diagram represents the grid. It can be shown that it is a valid configuration.
Input: grid =[[0,3,6],[5,8,1],[2,7,4]]Output: falseExplanation: The above diagram represents the grid. The 8th move of the knight is not valid considering its position after the 7th move.
A valid knight tour must start at (0,0) and each move from step k to k+1 must be a valid knight move. We can map each move number to its position and check all consecutive moves.
By storing the position of each move, we can check if each consecutive move is a valid knight move (i.e., the difference in coordinates is (2,1) or (1,2) in any direction). If all moves are valid, the configuration is valid.
classSolution {
public:bool checkValidGrid(vector<vector<int>>& grid) {
int n = grid.size();
vector<pair<int,int>> pos(n*n);
for (int i =0; i < n; ++i)
for (int j =0; j < n; ++j)
pos[grid[i][j]] = {i, j};
if (pos[0] != make_pair(0,0)) return false;
for (int k =0; k < n*n-1; ++k) {
int dx = abs(pos[k+1].first - pos[k].first);
int dy = abs(pos[k+1].second - pos[k].second);
if (!((dx ==1&& dy ==2) || (dx ==2&& dy ==1))) return false;
}
return true;
}
};
classSolution {
publicbooleancheckValidGrid(int[][] grid) {
int n = grid.length;
int[][] pos =newint[n*n][2];
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
pos[grid[i][j]]=newint[]{i, j};
if (pos[0][0]!= 0 || pos[0][1]!= 0) returnfalse;
for (int k = 0; k < n*n-1; k++) {
int dx = Math.abs(pos[k+1][0]- pos[k][0]);
int dy = Math.abs(pos[k+1][1]- pos[k][1]);
if (!((dx == 1 && dy == 2) || (dx == 2 && dy == 1))) returnfalse;
}
returntrue;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funcheckValidGrid(grid: Array<IntArray>): Boolean {
val n = grid.size
val pos = Array(n*n) { IntArray(2) }
for (i in0 until n) for (j in0 until n) pos[grid[i][j]] = intArrayOf(i, j)
if (pos[0][0] !=0|| pos[0][1] !=0) returnfalsefor (k in0 until n*n-1) {
val dx = Math.abs(pos[k+1][0] - pos[k][0])
val dy = Math.abs(pos[k+1][1] - pos[k][1])
if (!((dx ==1&& dy ==2) || (dx ==2&& dy ==1))) returnfalse }
returntrue }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
classSolution:
defcheck_valid_grid(self, grid: list[list[int]]) -> bool:
n = len(grid)
pos = [(-1, -1)] * (n * n)
for i in range(n):
for j in range(n):
pos[grid[i][j]] = (i, j)
if pos[0] != (0, 0):
returnFalsefor k in range(n * n -1):
dx = abs(pos[k+1][0] - pos[k][0])
dy = abs(pos[k+1][1] - pos[k][1])
ifnot ((dx ==1and dy ==2) or (dx ==2and dy ==1)):
returnFalsereturnTrue
impl Solution {
pubfncheck_valid_grid(grid: Vec<Vec<i32>>) -> bool {
let n = grid.len();
letmut pos =vec![(0, 0); n * n];
for i in0..n {
for j in0..n {
pos[grid[i][j] asusize] = (i asi32, j asi32);
}
}
if pos[0] != (0, 0) {
returnfalse;
}
for k in0..n * n -1 {
let dx = (pos[k +1].0- pos[k].0).abs();
let dy = (pos[k +1].1- pos[k].1).abs();
if!((dx ==1&& dy ==2) || (dx ==2&& dy ==1)) {
returnfalse;
}
}
true }
}