[leetcode] 1391. Check if There is a Valid Path in a Grid

Description

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

分析

题目的意思是:起点到终点是否存在一条路径可达。我参考了一下别人的思路和代码

  • 对于数组 grid 中的某个单元格 (x, y)(x,y),根据 grid[x][y] 值的不同,它最多会连接两个不同方向的单元格。
  • 实现四个函数 detectL(x, y)、detectR(x, y)、detectU(x, y) 以及 detectD(x, y),在 (x, y)(x,y) 连接了左、右、上、下四个方向时分别调用对应的函数。例如当 grid[x][y] 的值为 1 时,表示连接左右两个方向,那么调用 detectL(x, y) 和 detectR(x, y) 两个函数。
  • 以grid[x][y] 的值为 1 为例,当调用 detectL(x, y) 时,需要考虑左边的单元格 (x, y - 1),这个单元格必须能够与它的右侧,即 (x, y)相连,那么 grid[x][y - 1] 的值必须为 [1, 4, 6] 中的一个。
  • 遍历整个网格,对于其中的每一个单元格,根据它的 grid 值调用四个函数中的两个,并在满足要求时将该单元格与相邻的单元格相连(即在并查集中将它们合并)。在遍历结束之后,我们判断左上角和右下角在并查集中是否属于同一集合即可。

代码

class UnionFind:
    def __init__(self,size):
        self.f=list(range(size))
        
    def find(self,x):
        if(x==self.f[x]):
            return x
        self.f[x]=self.find(self.f[x])
        return self.f[x]
    
    def union(self,x,y):
        self.f[self.find(x)]=self.find(y)
    
class Solution:
    def getId(self,x,y):
        return x*self.n+y
    
    def detectL(self,uf,grid,x,y):
        if(y-1>=0 and grid[x][y-1] in [1,4,6]):
            uf.union(self.getId(x,y),self.getId(x,y-1))
    
    def detectR(self,uf,grid,x,y):
        if(y+1<len(grid[0]) and grid[x][y+1] in [1,3,5]):
            uf.union(self.getId(x,y),self.getId(x,y+1))
            
    def detectD(self,uf,grid,x,y):
        if(x+1<len(grid) and grid[x+1][y] in [2,5,6]):
            uf.union(self.getId(x,y),self.getId(x+1,y))
    
    def detectU(self,uf,grid,x,y):
        if(x-1>=0 and grid[x-1][y] in [2,3,4]):
            uf.union(self.getId(x,y),self.getId(x-1,y))
    
        
    def hasValidPath(self, grid: List[List[int]]) -> bool:
        m=len(grid)
        n=len(grid[0])
        uf=UnionFind(m*n)
        self.n=n
        for i in range(m):
            for j in range(n):
                if(grid[i][j]==1):
                    self.detectL(uf,grid,i,j)
                    self.detectR(uf,grid,i,j)
                elif(grid[i][j]==2):
                    self.detectU(uf,grid,i,j)
                    self.detectD(uf,grid,i,j)
                elif(grid[i][j]==3):
                    self.detectL(uf,grid,i,j)
                    self.detectD(uf,grid,i,j)
                elif(grid[i][j]==4):
                    self.detectR(uf,grid,i,j)
                    self.detectD(uf,grid,i,j)
                elif(grid[i][j]==5):
                    self.detectL(uf,grid,i,j)
                    self.detectU(uf,grid,i,j)
                elif(grid[i][j]==6):
                    self.detectR(uf,grid,i,j)
                    self.detectU(uf,grid,i,j)
                    
        return uf.find(self.getId(0,0))==uf.find(self.getId(m-1,n-1))

参考文献

检查网格中是否存在有效路径

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

农民小飞侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值