2596. 检查骑士巡视方案思路应该使用模拟的方法对比每次移动是否满足斜着走的规律即可为了方便比较每次移动路径,需要将矩阵转化为路径序列代码1234567891011121314151617181920212223242526class Solution { public boolean checkValidGrid(int[][] grid) { if (grid[0][0] != 0) { return false; } int n = grid.length; int[][] indices = new int[n * n][2]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { indices[grid[i][j]][0] = i; indices[grid[i][j]][1] = j; } } for (int i = 0; i < n * n - 1; i++) { if (!isDiagonal(indices[i], indices[i + 1])) { return false; } } return true; } boolean isDiagonal(int[] p1, int[] p2) { int diagonal = Math.abs((p1[0] - p2[0]) * (p1[1] - p2[1])); return diagonal == 2; }}