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))