-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1010 from 0xff-dev/1391
Add solution and test-cases for problem 1391
- Loading branch information
Showing
6 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
Binary file added
BIN
+20.4 KB
leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions
54
leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# [1391. Check if There is a Valid Path in a Grid][title] | ||
|
||
## Description | ||
You are given an `m x n` `grid`. Each cell of `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. | ||
|
||
![1](./1.png) | ||
|
||
You will initially start at the street of the upper-left cell `(0, 0)`. A valid path in the grid is a path that 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:** | ||
|
||
![e1](./e1.png) | ||
|
||
``` | ||
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:** | ||
|
||
![e2](./e1.png) | ||
|
||
``` | ||
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). | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/check-if-there-is-a-valid-path-in-a-grid | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
112 changes: 110 additions & 2 deletions
112
leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,113 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
var dirFn = [6]func(int, int, [][]int) [][2]int{ | ||
func(x, y int, grid [][]int) [][2]int { | ||
m, n := len(grid), len(grid[0]) | ||
res := make([][2]int, 0) | ||
|
||
nx, ny := x, y-1 | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 4 || grid[nx][ny] == 6) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
nx, ny = x, y+1 | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 3 || grid[nx][ny] == 5) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
return res | ||
}, | ||
func(x, y int, grid [][]int) [][2]int { | ||
m, n := len(grid), len(grid[0]) | ||
res := make([][2]int, 0) | ||
|
||
nx, ny := x-1, y | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 3 || grid[nx][ny] == 4) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
nx, ny = x+1, y | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 5 || grid[nx][ny] == 6) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
return res | ||
}, | ||
func(x, y int, grid [][]int) [][2]int { | ||
m, n := len(grid), len(grid[0]) | ||
res := make([][2]int, 0) | ||
|
||
nx, ny := x, y-1 | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 4 || grid[nx][ny] == 6) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
nx, ny = x+1, y | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 5 || grid[nx][ny] == 6) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
return res | ||
}, | ||
func(x, y int, grid [][]int) [][2]int { | ||
m, n := len(grid), len(grid[0]) | ||
res := make([][2]int, 0) | ||
|
||
nx, ny := x, y+1 | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 3 || grid[nx][ny] == 5) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
nx, ny = x+1, y | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 5 || grid[nx][ny] == 6) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
return res | ||
}, | ||
func(x, y int, grid [][]int) [][2]int { | ||
m, n := len(grid), len(grid[0]) | ||
res := make([][2]int, 0) | ||
|
||
nx, ny := x, y-1 | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 4 || grid[nx][ny] == 6) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
nx, ny = x-1, y | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 3 || grid[nx][ny] == 4) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
return res | ||
}, | ||
func(x, y int, grid [][]int) [][2]int { | ||
m, n := len(grid), len(grid[0]) | ||
res := make([][2]int, 0) | ||
|
||
nx, ny := x, y+1 | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 1 || grid[nx][ny] == 3 || grid[nx][ny] == 5) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
nx, ny = x-1, y | ||
if !(nx < 0 || nx >= m || ny < 0 || ny >= n) && (grid[nx][ny] == 2 || grid[nx][ny] == 3 || grid[nx][ny] == 4) { | ||
res = append(res, [2]int{nx, ny}) | ||
} | ||
return res | ||
}, | ||
} | ||
|
||
func Solution(grid [][]int) bool { | ||
m, n := len(grid)-1, len(grid[0])-1 | ||
q := [][2]int{{0, 0}} | ||
v := make(map[[2]int]struct{}) | ||
v[[2]int{0, 0}] = struct{}{} | ||
for len(q) > 0 { | ||
nq := make([][2]int, 0) | ||
for _, pos := range q { | ||
x, y := pos[0], pos[1] | ||
if x == m && y == n { | ||
return true | ||
} | ||
for _, next := range dirFn[grid[x][y]-1](x, y, grid) { | ||
if _, ok := v[next]; ok { | ||
continue | ||
} | ||
nq = append(nq, next) | ||
v[next] = struct{}{} | ||
} | ||
} | ||
q = nq | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+14.3 KB
leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/e1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+11 KB
leetcode/1301-1400/1391.Check-if-There-is-a-Valid-Path-in-a-Grid/e2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.