|
| 1 | +// Runtime: 116 ms (Top 86.9%) | Memory: 64.20 MB (Top 40.0%) |
| 2 | + |
1 | 3 | class Solution {
|
| 4 | + int n,m; |
| 5 | + bool vis[305][305]; |
| 6 | + vector<vector<int>>g; |
2 | 7 | public:
|
3 |
| - |
4 |
| - bool hasValidPath(vector<vector<int>>& grid) |
5 |
| - { |
6 |
| - int m = grid.size(); |
7 |
| - int n = grid[0].size(); |
8 |
| - if(m==1 and n==1) return true; |
9 |
| - // 2D - direction vector for all streets |
10 |
| - // 0th based indexing 0 to 11 |
11 |
| - |
12 |
| - // Let grid value g[i][j] = 4, means we need to follow "street-4" direction |
13 |
| - // First index of street-4 direction = 2*(4-1) = 6 |
14 |
| - // Second index of street-4 direction = 2*(4-1)+1 = 7 |
15 |
| - // dir[6] = {0, 1} |
16 |
| - // dir[7] = {1, 0} |
17 |
| - vector<vector<int>>dir = { // Indices |
18 |
| - {0,-1}, {0, 1}, // street 1 --> 0 1 |
19 |
| - {-1,0}, {1, 0}, // street 2 --> 2 3 |
20 |
| - {0,-1}, {1, 0}, // street 3 --> 4 5 |
21 |
| - {0, 1}, {1, 0}, // street 4 --> 6 7 |
22 |
| - {0,-1}, {-1,0}, // street 5 --> 8 9 |
23 |
| - {0, 1}, {-1,0} // street 6 --> 10 11 |
24 |
| - }; |
| 8 | + bool hasValidPath(vector<vector<int>>& grid) { |
| 9 | + n = grid.size(); |
| 10 | + m = grid[0].size(); |
| 11 | + g = grid; |
| 12 | + memset(vis, false, sizeof(vis)); |
| 13 | + dfs(0,0); |
| 14 | + return vis[n-1][m-1]; |
| 15 | + } |
| 16 | + |
| 17 | + void dfs(int ux, int uy){ |
| 18 | + vis[ux][uy]=true; |
| 19 | + int vx,vy; |
25 | 20 |
|
26 |
| - vector<vector<bool>>vis(m, vector<bool>(n, false)); |
27 |
| - queue<pair<int,int>>q; |
28 |
| - |
29 |
| - q.push({0, 0}); |
30 |
| - vis[0][0] = true; |
31 |
| - |
32 |
| - while (!q.empty()) |
33 |
| - { |
34 |
| - auto cur = q.front(); q.pop(); |
| 21 | + if(g[ux][uy] == 1 || g[ux][uy]==4 || g[ux][uy]==6) {vx=ux, vy=uy+1; if(valid(vx, vy) && L_enter(vx,vy)) dfs(vx, vy);} |
| 22 | + if(g[ux][uy] == 1 || g[ux][uy]==3 || g[ux][uy]==5) {vx=ux, vy=uy-1; if(valid(vx, vy) && R_enter(vx,vy)) dfs(vx, vy);} |
| 23 | + if(g[ux][uy] == 2 || g[ux][uy]==3 || g[ux][uy]==4) {vx=ux+1, vy=uy; if(valid(vx, vy) && D_enter(vx,vy)) dfs(vx, vy);} |
| 24 | + if(g[ux][uy] == 2 || g[ux][uy]==5 || g[ux][uy]==6) {vx=ux-1, vy=uy; if(valid(vx, vy) && U_enter(vx,vy)) dfs(vx, vy);} |
35 | 25 |
|
36 |
| - int r = cur.first; |
37 |
| - int c = cur.second; |
38 |
| - int val = grid[r][c] - 1; // grid values 1 to 6 |
39 |
| - |
40 |
| - if(r==m-1 and c==n-1) return true; |
41 |
| - |
42 |
| - // 2 directions from every cell |
43 |
| - for(int k=0;k<2;k++) // k = 0, k = 1 |
44 |
| - { |
45 |
| - int idx = 2*val+k; // get index |
46 |
| - int nr = r + dir[idx][0]; |
47 |
| - int nc = c + dir[idx][1]; |
48 |
| - if (nr < 0 or nr >= m or nc < 0 or nc >= n or vis[nr][nc]==true) continue; |
49 |
| - |
50 |
| - // for checking the back direction matches with current cell i.e forming path to next cell |
51 |
| - for(int x=0;x<2;x++) |
52 |
| - { |
53 |
| - int i = 2*(grid[nr][nc]-1)+x; // get index |
54 |
| - if(r == nr+dir[i][0] and c == nc+dir[i][1]){ |
55 |
| - vis[nr][nc] = true; |
56 |
| - q.push({nr, nc}); |
57 |
| - } |
58 |
| - } |
59 |
| - } |
60 |
| - } |
| 26 | + } |
| 27 | + bool valid(int x , int y){ |
| 28 | + if(x>=0 && y>=0 && x<n && y<m && !vis[x][y] ) return true; |
| 29 | + return false; |
| 30 | + } |
| 31 | + bool L_enter(int x, int y){ |
| 32 | + if(g[x][y]%2) return true; |
| 33 | + return false; |
| 34 | + } |
| 35 | + bool R_enter(int x, int y){ |
| 36 | + if(g[x][y]==1 || g[x][y]==4 || g[x][y]==6) return true; |
| 37 | + return false; |
| 38 | + } |
| 39 | + bool D_enter(int x, int y) |
| 40 | + { |
| 41 | + if(g[x][y]==2 || g[x][y]==5 || g[x][y]==6) return true; |
| 42 | + return false; |
| 43 | + } |
| 44 | + bool U_enter(int x, int y) |
| 45 | + { |
| 46 | + if(g[x][y]==2 || g[x][y]==3 || g[x][y]==4) return true; |
61 | 47 | return false;
|
62 | 48 | }
|
63 | 49 | };
|
0 commit comments