# 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 = [,,,,,,]
Output: true

Constraints:

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

## Solution

grid点仍然是grid

grid[i][j]点就变成了grid[2i][2j]

Union条件

if grid[i][j] in [2, 5, 6]: connect center and top
if grid[i][j] in [1, 3, 5]: connect center and left
if grid[i][j] in [2, 3, 4]: connect center and bottom
if grid[i][j] in [1, 4, 6]: connect center and right

uf是否与uf[2*(m-1), 2*(n-1)]相连

Time O(MN) * O(UF)
Space O(MN)

Python

class Solution(object):
def hasValidPath(self, grid):
m, n = len(grid), len(grid)
uf = {(i, j) : (i, j) for j in xrange(-1, 2*m + 1) for i in xrange(-1, 2*n + 1)}

def find(x):
while x != uf[x]:
uf[x] = uf[uf[x]]
x = uf[x]
return x

def union(x, y, dx, dy):
root = find((x, y))
droot = find((x + dx, y + dy))
if root != droot:
uf[root] = droot

for i in xrange(m):
for j in xrange(n):
if grid[i][j] in (2, 5, 6): union(2*i, 2*j, -1, 0)
if grid[i][j] in (2, 3, 4): union(2*i, 2*j, 1, 0)
if grid[i][j] in (1, 3, 5): union(2*i, 2*j, 0, -1)
if grid[i][j] in (1, 4, 6): union(2*i, 2*j, 0, 1)

return find((0, 0)) == find((2*m - 2, 2*n - 2))

Java

class Solution {
public boolean hasValidPath(int[][] grid) {
int m = grid.length;
int n = grid.length;
int t = 2*n + 1;
UF uf = new UF((2*m + 1)*(2*n + 1));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 1 || grid[i][j] == 3 || grid[i][j] == 5) {
uf.union(g(2*i + 1, 2*j + 1, t), g(2*i + 1, 2*j, t));
}
if (grid[i][j] == 1 || grid[i][j] == 4 || grid[i][j] == 6) {
uf.union(g(2*i + 1, 2*j + 1, t), g(2*i + 1, 2*j + 2, t));
}
if (grid[i][j] == 2 || grid[i][j] == 3 || grid[i][j] == 4) {
uf.union(g(2*i + 1, 2*j + 1, t), g(2*i + 2, 2*j + 1, t));
}
if (grid[i][j] == 2 || grid[i][j] == 5 || grid[i][j] == 6) {
uf.union(g(2*i + 1, 2*j + 1, t), g(2*i, 2*j + 1, t));
}
}
}
return uf.isConnected(g(1, 1, t), g(2*m - 1, 2*n - 1, t));
}

public int g(int r, int c, int n) {
return r*n + c;
}

class UF {
private int[] parents;

public UF(int n) {
parents = new int[n];

for (int i = 0; i < n; i++) {
parents[i] = i;
}
}

public int find(int node) {
while (node != parents[node]) {
parents[node] = parents[parents[node]];
node = parents[node];
}
return node;
}

public void union(int node1, int node2) {
int root1 = find(node1);
int root2 = find(node2);
if (root1 != root2) {
parents[root1] = root2;
}
}

public boolean isConnected(int node1, int node2) {
return find(node1) == find(node2);
}
}
}