Skip to content

Commit

Permalink
Create Readme.md
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Oct 22, 2018
1 parent d2a00d1 commit f5f9147
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions BFS/847.Shortest-Path-Visiting-All-Nodes/Readme.md
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中遇到重复的状态,毫无疑问,就没有继续搜索的必要了。

0 comments on commit f5f9147

Please sign in to comment.