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.lengthn == grid[i].length1 <= m, n <= 3001 <= grid[i][j] <= 6
Related Topics:
Depth-first Search, Breadth-first Search
// OJ: https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(MN)
class UnionFind {
private:
vector<int> id, rank;
int find (int i) {
if (id[i] == i) return i;
return id[i] = find(id[i]);
}
public:
UnionFind(int n) : id(n), rank(n, 0) {
for (int i = 0; i < n; ++i) id[i] = i;
}
void connect(int i, int j) {
int p = find(i), q = find(j);
if (p == q) return;
if (rank[p] > rank[q]) id[p] = q;
else {
id[q] = p;
if (rank[p] == rank[q]) rank[p]++;
}
}
bool connected(int i, int j) { return find(i) == find(j); }
};
class Solution {
int M, N;
int h(int x, int y) { return x * N + y; }
const int dirs[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};
const int neighbor[6][2] = {{0,1}, {2,3}, {0,3}, {1,3}, {0,2}, {1,2}};
public:
bool hasValidPath(vector<vector<int>>& A) {
M = A.size(), N = A[0].size();
UnionFind uf(M * N);
for (int i = 0; i < M; ++i) {
for (int j = 0; j < N; ++j) {
for (int n : neighbor[A[i][j] - 1]) {
int x = i + dirs[n][0], y = j + dirs[n][1];
if (x < 0 || x >= M || y < 0 || y >= N) continue;
int r = n ^ 1;
auto &rn = neighbor[A[x][y] - 1];
if (rn[0] != r && rn[1] != r) continue;
uf.connect(h(x, y), h(i, j));
}
}
}
return uf.connected(h(0,0), h(M-1,N-1));
}
};// OJ: https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid/
// Author: github.com/lzl124631x
// Time: O(MN)
// Space: O(MN)
// Ref: https://www.youtube.com/watch?v=SpMez87v0O8
class Solution {
int M, N;
vector<vector<int>> A;
vector<vector<bool>> vis;
int h(int x, int y) { return x * N + y; }
int dx[4] = {1,-1,0,0}, dy[4] = {0,0,1,-1}, t[6] = {4|8, 1|2, 8|1, 4|1, 8|2, 4|2};
bool dfs(int i, int j) {
if (i == M - 1 && j == N - 1) return 1;
vis[i][j] = 1;
for (int k = 0; k < 4; ++k) {
if (t[A[i][j] - 1] >> k & 1 ^ 1) continue; // If A[i][j] can't extend to this direction, skip
int x = i + dx[k], y = j + dy[k];
if (x < 0 || x >= M || y < 0 || y >= N || vis[x][y]) continue;
int rk = k ^ 1;
if (t[A[x][y] - 1] >> rk & 1 ^ 1) continue; // If A[x][y] can't extend back, skip
if (dfs(x, y)) return 1;
}
return 0;
}
public:
bool hasValidPath(vector<vector<int>>& A) {
M = A.size(), N = A[0].size();
this->A = A;
vis.assign(M, vector<bool>(N));
return dfs(0, 0);
}
};

