All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2501_2600.s2596_check_knight_tour_configuration.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g2501_2600.s2596_check_knight_tour_configuration;

// #Medium #Array #Depth_First_Search #Breadth_First_Search #Matrix #Simulation
// #2023_08_29_Time_1_ms_(100.00%)_Space_43.4_MB_(42.62%)

/**
 * 2596 - Check Knight Tour Configuration\.
 *
 * Medium
 *
 * 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. The figure below illustrates all the possible eight moves of a knight from some cell.
 *
 * ![](https://assets.leetcode.com/uploads/2018/10/12/knight.png)
 *
 * **Example 1:**
 *
 * ![](https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-5.png)
 *
 * **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:** true
 *
 * **Explanation:** The above diagram represents the grid. It can be shown that it is a valid configuration.
 *
 * **Example 2:**
 *
 * ![](https://assets.leetcode.com/uploads/2022/12/28/yetgriddrawio-6.png)
 *
 * **Input:** grid = \[\[0,3,6],[5,8,1],[2,7,4]]
 *
 * **Output:** false
 *
 * **Explanation:** The above diagram represents the grid. The 8th move of the knight is not valid considering its position after the 7th move.
 *
 * **Constraints:**
 *
 * *   `n == grid.length == grid[i].length`
 * *   `3 <= n <= 7`
 * *   `0 <= grid[row][col] < n * n`
 * *   All integers in `grid` are **unique**.
**/
public class Solution {
    public boolean checkValidGrid(int[][] grid) {
        if (grid[0][0] != 0) {
            return false;
        }
        int n = grid.length;
        int m = grid[0].length;
        int[] rmove = {2, 2, -2, -2, 1, 1, -1, -1};
        int[] cmove = {1, -1, 1, -1, 2, -2, 2, -2};
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                int val = grid[i][j];
                boolean isPoss = false;
                for (int d = 0; d < 8; d++) {
                    int r = i + rmove[d];
                    int c = j + cmove[d];
                    if (r >= 0 && c >= 0 && r < n && c < m && grid[r][c] == val + 1) {
                        isPoss = true;
                    }
                }
                if (!isPoss) {
                    cnt++;
                }
                if (cnt > 1) {
                    return false;
                }
            }
        }
        return true;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy