forked from wisdompeak/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d2a00d1
commit f5f9147
Showing
1 changed file
with
7 additions
and
0 deletions.
There are no files selected for viewing
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,7 @@ | ||
### 847.Shortest-Path-Visiting-All-Nodes | ||
|
||
如果我们想找一条满足题意的路径,该怎么做呢?无非就是搜索嘛。但无论是DFS还是BFS,需要考虑的一个重要问题就是如何去重,否则就会无休止地搜索下去。此题中,我们应该如何记录“状态”呢?显然,我们不能单纯地将某个node,或者某条edge是否访问过作为存储的状态,因为我们极有可能需要访问某个node或edge多次。 | ||
|
||
那么我们是否可以将所有访问过的node的集合作为一个状态呢?这样也是不行的。比如考虑这个网络“0-1,0-2,0-3”,我们访问完了0,1之后,不得不返回0再去访问其他的节点,这个过程中,node的集合其实是不变的。如果我们将node的集合作为状态存储起来做去重的操作,就会被舍弃掉正确的方案。 | ||
|
||
所以,正确的状态应该是“当前所在节点+已经访问过的node的集合”。依据这种定义,如果在BFS中遇到重复的状态,毫无疑问,就没有继续搜索的必要了。 |