Skip to problem
←

Corner-to-Center Maze Paths

Hard 0 attempts

Find every path from any corner of an odd-sized square maze to its center at (Math.floor(n / 2), Math.floor(n / 2)).

From (r, c), jump exactly maze[r][c] cells up, down, left, or right. The landing cell must be inside the maze and must not already occur in the current path. Only landing cells are visited; cells jumped over are not added to the path. A zero-valued cell cannot move.

Stop a path when it first reaches the center, regardless of the center’s value. For n = 1, return the single path [[0,0]] once, wrapped in the result array: [[[0,0]]]. Return an empty array if no corner can reach the center.

Function contract

maze is a nonempty n × n matrix with odd n. Each entry is a nonnegative integer jump length.

Output: Return an array of paths. Each path is an ordered array of zero-based [row, column] coordinates, including its corner and the center. Path order does not matter; do not duplicate paths.

Example 1

Input
maze = [[1,1,0],[0,0,0],[0,0,0]]
Output
[[[0,0],[0,1],[1,1]]]

Explanation: Start at the top-left corner, jump right once, then down once to the center. Other corners cannot move.

Example 2

Input
maze = [[0]]
Output
[[[0,0]]]

Example 3

Input
maze = [[0,0,0],[0,0,0],[0,0,0]]
Output
[]
Visual Aid
One valid route
110000000

(0,0) → (0,1) → (1,1). Stop at the center even though its value is 0.

Constraints
  • 1 <= n <= 9
  • n is odd
  • maze is square; 0 <= maze[r][c] <= 10^4.

Approach: DFS Backtracking

Start DFS from each corner cell. At each cell, try jumping exactly maze[i][j] steps in all four directions. Mark cells as visited to avoid cycles, and backtrack when a direction leads nowhere. Collect the path when you reach the center.

javascript
function findPaths(maze) {
  const n = maze.length;
  const mid = Math.floor(n / 2);
  const results = [];
  const corners =
    n === 1
      ? [[0, 0]]
      : [
          [0, 0],
          [0, n - 1],
          [n - 1, 0],
          [n - 1, n - 1],
        ];
  const dirs = [
    [0, 1],
    [0, -1],
    [1, 0],
    [-1, 0],
  ];

  function dfs(r, c, path, visited) {
    if (r === mid && c === mid) {
      results.push([...path]);
      return;
    }

    const steps = maze[r][c];
    for (const [dr, dc] of dirs) {
      const nr = r + dr * steps;
      const nc = c + dc * steps;
      if (nr < 0 || nr >= n || nc < 0 || nc >= n) continue;
      const key = nr * n + nc;
      if (visited.has(key)) continue;

      visited.add(key);
      path.push([nr, nc]);
      dfs(nr, nc, path, visited);
      path.pop();
      visited.delete(key);
    }
  }

  for (const [sr, sc] of corners) {
    const key = sr * n + sc;
    dfs(sr, sc, [[sr, sc]], new Set([key]));
  }
  return results;
}

Time: O(4^(n²) + Pn²) as a loose worst-case bound, where P is the number of returned paths. Copying the paths can itself be expensive; this is an exhaustive enumeration problem.

Space: O(n²) auxiliary space for the current path, visited set, and recursion, plus O(Pn²) for the returned paths in the worst case.

Loading discussion…

Saved in this browser only. Private to you.

JavaScript