Skip to content

Commit

Permalink
Merge pull request #1010 from 0xff-dev/1391
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1391
  • Loading branch information
6boris authored Oct 30, 2024
2 parents c46f96e + bb2a683 commit 5c5ad4d
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 8 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
inputs [][]int
expect bool
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{{2, 4, 3}, {6, 5, 2}}, true},
{"TestCase2", [][]int{{1, 2, 1}, {1, 2, 1}}, false},
{"TestCase3", [][]int{{1, 1, 2}}, false},
}

// 开始测试
Expand All @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) {
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5c5ad4d

Please sign in to comment.