Gamified challenge · Backtracking
A knight stands on square (startRow, startCol) of an n × n board (rows and columns 0 to n - 1). Each jump moves it 2 squares one way and 1 square the other (an L). It must land on every square exactly once; it doesn't need to return to the start.
Write tour(n, startRow, startCol) that returns the board as n rows of n numbers, where board[r][c] is the move number on which the knight lands on (r, c): 0 for the start square, then 1, 2, … up to n² - 1. Any valid tour passes. If no tour exists (for example on a 3 × 3 or 4 × 4 board), return an empty list [].
Boards go up to 8 × 8, where blind backtracking is far too slow: choose the next square cleverly.
This game is part of the paid plans. Sign in, then pick a plan to write your answer, watch it play out, and step through the algorithm with its guide.
Sign in with Google