Skip to content

1391. Check if There is a Valid Path in a Grid

Description

You are given an m x n grid. Each cell of grid represents a street. The street of grid[i][j] can be:

  • 1 which means a street connecting the left cell and the right cell.
  • 2 which means a street connecting the upper cell and the lower cell.
  • 3 which means a street connecting the left cell and the lower cell.
  • 4 which means a street connecting the right cell and the lower cell.
  • 5 which means a street connecting the left cell and the upper cell.
  • 6 which means a street connecting the right cell and the upper cell.

 

 

You will initially start at the street of the upper-left cell (0, 0). A valid path in the grid is a path that starts from the upper left cell (0, 0) and ends at the bottom-right cell (m - 1, n - 1). The path should only follow the streets.

Notice that you are not allowed to change any street.

Return true if there is a valid path in the grid or false otherwise.

 

Example 1:

 

Input: grid = [[2,4,3],[6,5,2]]
Output: true
Explanation: As shown you can start at cell (0, 0) and visit all the cells of the grid to reach (m - 1, n - 1).

Example 2:

 

Input: grid = [[1,2,1],[1,2,1]]
Output: false
Explanation: As shown you the street at cell (0, 0) is not connected with any street of any other cell and you will get stuck at cell (0, 0)

Example 3:

Input: grid = [[1,1,2]]
Output: false
Explanation: You will get stuck at cell (0, 1) and you cannot reach cell (0, 2).

 

Constraints:

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 300
  • 1 <= grid[i][j] <= 6

 

Solutions

Solution: Depth-First Search

  • Time complexity: O(m*n)
  • Space complexity: O(m*n)

 

JavaScript

js
/**
 * @param {number[][]} grid
 * @return {boolean}
 */
const hasValidPath = function (grid) {
  const m = grid.length;
  const n = grid[0].length;
  const start = grid[0][0];

  if (start === 5) return false;

  const streetMap = {
    1: (from, row, col) => {
      if (from === 'up' || from === 'down') return false;

      if (from === 'left') return { row, col: col + 1, to: 'left' };

      return { row, col: col - 1, to: 'right' };
    },
    2: (from, row, col) => {
      if (from === 'left' || from === 'right') return false;

      if (from === 'up') return { row: row + 1, col, to: 'up' };

      return { row: row - 1, col, to: 'down' };
    },
    3: (from, row, col) => {
      if (from === 'up' || from === 'right') return false;

      if (from === 'left') return { row: row + 1, col, to: 'up' };

      return { row, col: col - 1, to: 'right' };
    },
    4: (from, row, col) => {
      if (from === 'up' || from === 'left') return false;

      if (from === 'right') return { row: row + 1, col, to: 'up' };

      return { row, col: col + 1, to: 'left' };
    },
    5: (from, row, col) => {
      if (from === 'down' || from === 'right') return false;

      if (from === 'up') return { row, col: col - 1, to: 'right' };

      return { row: row - 1, col, to: 'down' };
    },
    6: (from, row, col) => {
      if (from === 'down' || from === 'left') return false;

      if (from === 'up') return { row, col: col + 1, to: 'left' };

      return { row: row - 1, col, to: 'down' };
    },
  };

  const isValidPath = (row, col, from) => {
    if (row < 0 || col < 0 || row >= m || col >= n) return false;

    const current = grid[row][col];

    if (current === 'x') return false;

    const nextPos = streetMap[current](from, row, col);

    if (!nextPos) return false;

    if (row === m - 1 && col === n - 1) return true;

    grid[row][col] = 'x';

    const isValid = isValidPath(nextPos.row, nextPos.col, nextPos.to);

    grid[row][col] = current;

    return isValid;
  };

  if (start === 4) {
    return isValidPath(0, 0, 'right') || isValidPath(0, 0, 'down');
  }

  const fromMap = { 1: 'left', 2: 'up', 3: 'left', 6: 'up' };

  return isValidPath(0, 0, fromMap[start]);
};

Released under the MIT license