LeetCode-Python-1391. Check if there is a valid path (BFS) in the grid

Give you an mxn grid grid. Each cell in the grid represents a street. The streets of grid [i] [j] can be:

1 represents the street connecting the left and right cells.
2 indicates the street connecting the upper cell and the lower cell.
3 indicates the street connecting the left cell and the lower cell.
4 indicates the street connecting the right cell and the lower cell.
5 represents the street connecting the left cell and the upper cell.
6 indicates the street connecting the right cell and the upper cell.


You start from the cell (0,0) in the upper left corner. The "effective path" in the grid refers to the cell (0,0) from the upper left to the lower right (m-1, n-1) The ending path. The path must only go along the street.

Note: You cannot change the street.

If there is a valid path in the grid, return true, otherwise return false.

 

Example 1:

Input: grid = [[2,4,3], [6,5,2]]
Output: true
Explanation: As shown in the figure, you can start from (0, 0), access all the cells in the grid and Reach (m-1, n-1).
Example 2:

Input: grid = [[1,2,1], [1,2,1]]
Output: false
Explanation: As shown in the figure, the street on cell (0, 0) is not the same as the street on any other cell Connected, you will only stop at (0, 0).
Example 3:

Input: grid = [[1,1,2]]
Output: false
Explanation: You will stop at (0, 1) and cannot reach (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
 

prompt:

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

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/check-if-there-is-a-valid-path-in-a-grid
copyrighted by deduction from all networks. Please contact the official authorization for commercial reprint, and please indicate the source for non-commercial reprint.

Ideas:

First define the six kinds of streets given by the title. For each street, use an array of length 4. Each subscript represents the northeast, southeast and west connectivity in order,

dir [1] = [0, 1, 0, 1] means that street 1 has only east-west connectivity.

Then define the corresponding direction, such as going north to the south of the next street, and so on:

cor = {0: 2, 1: 3, 3: 1, 2: 0}, because I defined the subscript 0 for north, 1 for east, 2 for south, and 3 for west

Then the ordinary BFS starts to go,

The starting position is [0, 0],

Transfer is to see if you can go to the adjacent grid, and the judgment basis for whether you can go is:

1. Legal coordinates,

2. Never walked

3. And the streets are connected to each other (here I use & to judge 1),

The termination condition depends on whether you can go to [m-1, n-1], or you have no way to go.

Time complexity: O (MN)

Space complexity: O (MN)

class Solution(object):
    def hasValidPath(self, grid):
        """
        :type grid: List[List[int]]
        :rtype: bool
        """
        m, n = len(grid), len(grid[0])
        dir = {}
        dir[1] = [0, 1, 0, 1]
        dir[2] = [1, 0, 1, 0]
        dir[3] = [0, 0, 1, 1]
        dir[4] = [0, 1, 1, 0]
        dir[5] = [1, 0, 0, 1]
        dir[6] = [1, 1, 0, 0]

        dx = [-1, 0, 1, 0]
        dy = [0, 1, 0, -1]
        queue = [(0, 0)]
        visited = set(queue)
        
        cor = {0:2, 1:3, 3:1, 2:0}
        while queue:
            x0, y0 = queue.pop()
            if [x0, y0] == [m - 1, n - 1]:
                return True
            for k in range(4):
                x = x0 + dx[k]
                y = y0 + dy[k]
                if 0 <= x < m and 0 <= y < n and (x, y) not in visited and dir[grid[x0][y0]][k] & dir[grid[x][y]][cor[k]]:
                    visited.add((x, y))
                    queue.append((x, y))
        return False

 

Published 734 original articles · 121 praises · 210,000 views

Guess you like

Origin blog.csdn.net/qq_32424059/article/details/105138757