给定一个蛇梯板,我们必须找到最后一个顶点到第0个顶点的最小距离。(在第0个顶点,我们掷骰子并向前移动)
请阅读此处的问题->LINK
我的代码:
from collections import defaultdict
global INT_MAX
INT_MAX = 3 ** 38
class Graph:
def __init__(self):
self.vertList = defaultdict(list)
def addEdge(self, u, v):
self.vertList[u].append(v)
def distanceBFS(self, src, target):
queue = []
queue.append(src)
distanceDict = {}
for v in self.vertList:
distanceDict[v] = INT_MAX
distanceDict[src] = 0
visited = set()
while (len(queue) > 0):
curr = queue.pop(0)
visited.add(curr)
for vertex in self.vertList[curr]:
if vertex not in visited:
queue.append(vertex)
distanceDict[vertex] = distanceDict[curr] + 1
print(distanceDict[target])
def solveSnakesLadder(self):
snakeLadderDict = {2: 13, 5: 2, 9: 18, 18: 11, 17: -13, 20: -14, 24: -8, 25: 10, 32: -2, 34: -22}
# we have 36 boxes --> i
# we can throw a dice at each of these 36 boxes and it can be from 1 to 6
# j is new position on the board after throwing a dice
for i in range(0, 37):
for dice in range(1, 7):
j = i + dice
if j in snakeLadderDict:
j += snakeLadderDict[j]
if j <= 36:
self.addEdge(i, j)
self.distanceBFS(0, 36)
g = Graph()
g.solveSnakesLadder()
电流输出:
10
正确输出:
4
我做错了什么?这和逻辑有关吗?在添加到队列中之前,我还可以检查distanceDict中的值,但是访问集也会做同样的事情
您的算法中有两个错误。最重要的是:
这可能会用更差的距离覆盖现有距离。所以你应该:
并且,
distanceDict
中缺少一个顶点,因为节点36没有传出边。这可能不是一个真正的错误,而是一个遗漏。但是,在图形中为所有节点设置一个条目是合乎逻辑的。因此,您应该处理这个问题,例如通过初始化:找到目标后退出循环也是一个好主意,这样可以避免不必要地访问所有其他节点,这永远不会改变结果
最后,不需要跟踪每个节点与源的距离,也不需要使用队列。如果分隔BFS遍历树的每个“级别”,则可以为每个级别使用新列表,并增加单个距离变量以跟踪它
请注意,此函数返回距离,因此仍需打印:
基本上,问题在于以下代码块:
我将尝试用以下基本图表来解释这一点:
假设,您需要在下图中找到从顶点
1
到顶点3
的最短路径。使用上述实现,您将有以下迭代:1
添加到队列Q
,并将其距离设置为0
while
循环中从Q
弹出1
,并将其标记为已访问Q
李>因此,此时,不同对象的状态如下所示:
我们在队列
Q
中有顶点2
和3
,而distance
字典将作为distance[1] = 0, distance[2] = 1, distance[3] = 1
,这就是问题的开始,因为我们还没有将顶点2
和3
添加到visited
如果我们从其他邻居处再次访问这些顶点,我们将在下一次迭代中再次开始更新这些顶点的距离while
循环的开始,从它开始pop
顶点2
,并将其标记为已访问李>3
,并将使用不正确的值更新distance
字典李>访问vertex
2
及其相邻对象后,不同对象的状态如下:我们在队列中有顶点
3
,距离字典将为distance[1] = 0, distance[2] = 1, distance[3] = 2
distance[3]
被错误地更新了,正如我们在迭代中看到的那样然而,修复是非常直接的,在这里,我们需要在访问顶点之后将其标记为已访问,这样我们就不会通过其他邻居将其再次放入队列中
到目前为止,您已经猜到BFS函数的正确实现应该如下所示:
相关问题 更多 >
编程相关推荐