Skip to problem
←

Minimum Knight Moves on a Board

Medium 0 attempts

On an empty n × n chessboard, find the fewest knight moves from src to dest.

Both positions use zero-based [row, column] coordinates, ranging from 0 to n - 1. In one move, change one coordinate by 2 and the other by 1, independently choosing either sign. The landing square must remain on the board.

For example, from [0,0] on a board of size at least 3, the legal destinations are [1,2] and [2,1].

Return 0 when the source already equals the destination. Return -1 if no legal sequence reaches the destination; this can happen on small boards.

Function contract

Implement minSteps(n, src, dest).

Output: Return the fewest legal knight moves, 0 if src equals dest, or -1 if the destination is unreachable.

Example 1

Input
n = 6
src = [3,4]
dest = [0,0]
Output
3

Example 2

Input
n = 1
src = [0,0]
dest = [0,0]
Output
0

Example 3

Input
n = 2
src = [0,0]
dest = [1,1]
Output
-1
Constraints
  • 1 <= n <= 1000
  • src and dest each contain two integers: [row, column]
  • 0 <= src[i], dest[i] < n

Breadth-First Search

Every legal move has equal cost, so BFS visits squares in increasing move count. Mark a square when it is enqueued to avoid repeated work. Process one queue layer per distance.

Encode [row, column] as row * n + column. Typed arrays store visited flags and queue entries compactly, avoiding a separate coordinate array for each of up to one million squares.

javascript
function minSteps(n, src, dest) {
  if (src[0] === dest[0] && src[1] === dest[1]) return 0;
  const moves = [[-2,-1],[-2,1],[-1,-2],[-1,2],[1,-2],[1,2],[2,-1],[2,1]];
  const visited = new Uint8Array(n * n);
  const queue = new Int32Array(n * n);
  const start = src[0] * n + src[1];
  const target = dest[0] * n + dest[1];
  let head = 0, tail = 1, distance = 0;
  queue[0] = start;
  visited[start] = 1;
  while (head < tail) {
    const end = tail;
    distance++;
    while (head < end) {
      const cell = queue[head++];
      const row = Math.floor(cell / n), col = cell % n;
      for (const [dr, dc] of moves) {
        const r = row + dr, c = col + dc;
        if (r < 0 || r >= n || c < 0 || c >= n) continue;
        const next = r * n + c;
        if (visited[next]) continue;
        if (next === target) return distance;
        visited[next] = 1;
        queue[tail++] = next;
      }
    }
  }
  return -1;
}

Time: O(n²), with at most eight neighbors per square.

Space: O(n²). The typed-array storage uses about 5 MB at n = 1000.

Loading discussion…

Saved in this browser only. Private to you.

JavaScript