674. The Knight's Tour
Given a pair of positive integers (r, c) represent the knight's initial location on a 0-indexed 2D-array board, given two positive integers (m and n) that represent the board's height and width.
Your task is to arrange the knight's motions so that each cell on the board is visited precisely once (the starting cell is regarded as visited and should not be visited again).
Return the array board where the values of the cells indicate the sequence in which the cells should be visited, beginning at 0 (the knight's starting position).
Note that a knight can move from cell (r1, c1) to cell (r2, c2) if 0 <= r2 <= m - 1 and 0 <= c2 <= n - 1 and min(abs(r1 - r2), abs(c1 - c2)) = 1 and max(abs(r1 - r2), abs(c1 - c2)) = 2.
Example 1:
Input : m = 3, n = 4, r = 0, c = 0
Output : [ [0, 3, 6, 9], [7, 10, 1, 4], [2, 5, 8, 11] ]
Explanation :
By the following order of movements we can visit the entire board.
(0,0)->(1,2)->(2,0)->(0,1)->(1,3)->(2,1)->(0,2)->(1,0)->(2,2)->(0,3)->(1,1)->(2,3)
Example 2:
Input : m = 1, n = 1, r = 0, c = 0
Output : [ [0] ]
Explanation :
The board consist of only one cell which is thee starting cell and already visited.
Now Your Turn!
Pick the correct output for the given inputInput : m = 3, n = 4, r = 0, c = 3
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= m, n <= 5
- 0 <= r <= m - 1
- 0 <= c <= n - 1
- The inputs will be generated such that there exists at least one possible order of movements with the given condition