Knight moves
Standard · Write code · about 10 min
A knight stands on square (r, c) of an n × n board. In one jump it moves two squares in one direction and one square in the other.
Return every square it can reach in one jump, as [row, column] pairs, in any order.
Example 1
Input:
[5,0,1]
Output:
[[1,3],[2,2],[2,0]]
Five other jumps leave the board.
Example 2
Input:
[3,1,1]
Output:
[]
Every jump leaves the board.
Constraints
- 1 ≤ n ≤ 50
- 0 ≤ r, c < n
Learn the idea first: Implicit graphs: grids, states and puzzles, a visual lesson.
More Implicit graphs: grids, states and puzzles problems
- Neighbors of a cell Warm-up
- Count islands preview Warm-up
- Fewest operations Standard
- Fewest knight moves Standard
- Model a puzzle as a graph Challenge