In chess, a "Knight's Tour" is a sequence of moves where a knight visits every square on an board exactly once. You are given an grid where each cell contains a number from 0 to , representing the order in which the knight visited that square. You need to determine if the given grid represents a valid knight's tour, starting from .
Microsoft, Meta, and Google use this to test your ability to perform Simulation and Matrix traversal. It’s a test of whether you can correctly implement the move rules of a specific game (the L-shape move of a knight) and verify a sequence of events. It evaluation your attention to grid boundaries and movement logic.
The pattern is Simulation. You start at the cell containing 0 (which must be ). Then, you find the location of the next number (1, 2, 3...) and check if the move from the current cell to the next cell is a valid knight's move. A knight move is valid if the absolute difference in rows is 1 and columns is 2, or vice versa ().
Grid: 0 3 6 5 8 1 2 7 4
A common error is not checking if the tour starts at . Another is inefficiently searching for the next number in every step ( search for each of numbers results in ). Instead, you should pre-store the coordinates of each number in an array to make the checks each, leading to a total time of .
For simulation problems where you need to visit elements in a specific order, always consider "preprocessing" the locations. Storing pos[value] = (row, col) turns a search problem into a simple iteration, which is much more efficient.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Contain Virus | Hard | Solve | |
| Find All Groups of Farmland | Medium | Solve | |
| Coloring A Border | Medium | Solve | |
| Minesweeper | Medium | Solve | |
| Pacific Atlantic Water Flow | Medium | Solve |