Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add solution and test-cases for problem 847 #627

Merged
merged 1 commit into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 15 additions & 14 deletions leetcode/801-900/0847.Shortest-Path-Visiting-All-Nodes/README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
# [847.Shortest Path Visiting All Nodes][title]

> [!WARNING|style:flat]
> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

## Description
You have an undirected, connected graph of `n` nodes labeled from `0` to `n - 1`. You are given an array `graph` where `graph[i]` is a list of all the nodes connected with node `i` by an edge.

Return the length of the shortest path that visits every node. You may start and stop at any node, you may revisit nodes multiple times, and you may reuse edges.

**Example 1:**

**Example 1:**
![example1](./shortest1-graph.jpeg)

```
Input: a = "11", b = "1"
Output: "100"
Input: graph = [[1,2,3],[0],[0],[0]]
Output: 4
Explanation: One possible path is [1,0,2,0,3]
```

## 题意
> ...
**Example 2:**

## 题解
![example2](./shortest2-graph.jpeg)

### 思路1
> ...
Shortest Path Visiting All Nodes
```go
```

Input: graph = [[1],[0,2,4],[1,3,4],[2],[1,2]]
Output: 4
Explanation: One possible path is [0,1,4,2,3]
```

## 结语

Expand Down
34 changes: 32 additions & 2 deletions leetcode/801-900/0847.Shortest-Path-Visiting-All-Nodes/Solution.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
package Solution

func Solution(x bool) bool {
return x
func Solution(graph [][]int) int {
queue := [][]int{}
size := len(graph)

finalState := (1 << size) - 1
visited := make(map[[2]int]struct{})
for i := 0; i < size; i++ {
queue = append(queue, []int{i, 1 << i})
visited[[2]int{i, 1 << i}] = struct{}{}
}

ans := -1
for len(queue) > 0 {
nq := make([][]int, 0)
ans++
for _, item := range queue {
node := item[0]
state := item[1]
for _, next := range graph[node] {
nextState := state | (1 << next)
if nextState == finalState {
return ans + 1
}
if _, ok := visited[[2]int{next, nextState}]; !ok {
nq = append(nq, []int{next, nextState})
visited[[2]int{next, nextState}] = struct{}{}
}
}
}
queue = nq
}
return 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
inputs [][]int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", [][]int{
{1, 2, 3}, {0}, {0}, {0},
}, 4},
{"TestCase2", [][]int{
{1}, {0, 2, 4}, {1, 3, 4}, {2}, {1, 2},
}, 4},
}

// 开始测试
Expand All @@ -30,10 +33,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.
Loading