【leetcode】1391. Check if There is a Valid Path in a Grid

Topics are as follows:

Given a m x n grid. Each cell of the 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 which 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).

Example 4:

Input: grid = [[1,1,1,1,1,1,3]]
Output: true

Example 5:

Input: grid = [[2],[2],[2],[2],[2],[2],[6]]
Output: true 

Constraints:

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

Outline of Solution: The key question is to determine the direction of movement, such movement may be left on the butt 1, 4, 6; 4 can butt 2,5,6 downward movement. Also out is the direction of, for example, from the left into the 5, you can only go out from above. If there is a path in line with requirements of the subject, then this must be the only path to determine this, just start from the beginning in order to try to.

code show as below:

class Solution(object):
    def hasValidPath(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: bool
        """
        dic_pair = {}
        dic_pair[(1,'L')] = [1,4,6]
        dic_pair[(1, 'R')] = [1,3,5]
        dic_pair[(2,'D')] = [2,5,6]
        dic_pair[(2,'U')] = [2,3,4]
        dic_pair[(3,'L')] = [1,4,6]
        dic_pair[(3,'D')] = [2,5,6]
        dic_pair[(4, 'R')] = [1,3,5]
        dic_pair[(4, 'D')] = [2,5,6]
        dic_pair[(5, 'U')] = [2,3,4]
        dic_pair[(5, 'L')] = [1,4, 6]
        dic_pair[(6, 'U')] = [2,3,4]
        dic_pair[(6, 'R')] = [1,3,5]

        dic_dir = {}
        dic_dir [ 1] = [ ' L ' , ' R ' ]
        dic_dir [ 2] = [ ' U ' , ' D ' ]
        dic_dir [ 3] = [ ' L ' , ' D ' ]
        dic_dir [ 4] = [ ' A ' , ' D ' ]
        dic_dir [ 5] = [ ' L ' , ' U ' ]
        dic_dir[6] = ['R', 'U']

        queue = []

        def verify():
            #queue = []

            dic_visit = {}
            dic_visit[(0,0)] = 1

            direction = {}
            direction['R'] = (0,1)
            Management [ ' L ' ] = (0, -1 )
            direction['U'] = (-1,0)
            Management [ ' D ' ] = (1 , 0)

            while len(queue) > 0:
                x,y,d = queue.pop(0)
                #print x,y
                if x == len(grid)-1 and y == len(grid[0]) - 1:
                    return True

                x1,y1 = direction[d]

                if x1 + x >= 0 and x1 + x < len(grid) and y + y1 >= 0 and y + y1 < len(grid[0]) \
                        and grid[x1 + x][y1 + y] in dic_pair[(grid[x][y], d)] and (x1 + x, y1 + y) not in dic_visit:
                    if d == 'L':
                        reversed_d = 'R'
                    elif d == 'R':
                        reversed_d = 'L'
                    elif d == 'U':
                        reversed_d = 'D'
                    else:
                        reversed_d = 'U'
                    inx = dic_dir[grid[x1 + x][y1 + y]].index(reversed_d)
                    if inx == 0:
                        d1 = dic_dir[grid[x1 + x][y1 + y]][1]
                    else:
                        d1 = dic_dir[grid[x1 + x][y1 + y]][0]
                    queue.append((x1 + x, y1 + y, d1))
                    dic_visit[(x1 + x, y1 + y)] = 1
            return False

        if grid[0][0] == 1:
            queue.append((0, 0, 'R'))
        elif grid[0][0] == 2:
            queue.append((0, 0, 'D'))
        elif grid[0][0] == 3:
            queue.append((0, 0, 'D'))
        elif grid[0][0] == 4:
            queue.append((0, 0, 'D'))
        elif grid[0][0] == 6:
            queue.append((0, 0, 'R'))
        return verify()

 

Guess you like

Origin www.cnblogs.com/seyjs/p/12590740.html
Recommended