显示回退路径
This commit is contained in:
parent
460256c1cb
commit
67d8e66e57
@ -119,7 +119,7 @@ class SourceCollector:
|
|||||||
u_to_lca = []
|
u_to_lca = []
|
||||||
node = u
|
node = u
|
||||||
while node != lca:
|
while node != lca:
|
||||||
u_to_lca.append(node.pos)
|
u_to_lca.append(node)
|
||||||
node = node.fa
|
node = node.fa
|
||||||
|
|
||||||
lca_to_v = []
|
lca_to_v = []
|
||||||
@ -131,37 +131,80 @@ class SourceCollector:
|
|||||||
node_list.append(lca)
|
node_list.append(lca)
|
||||||
node_list.reverse()
|
node_list.reverse()
|
||||||
for node in node_list:
|
for node in node_list:
|
||||||
lca_to_v.append(node.pos)
|
lca_to_v.append(node)
|
||||||
|
|
||||||
full_path = u_to_lca + lca_to_v
|
|
||||||
|
full_path = u_to_lca + lca_to_v[:-1]
|
||||||
return full_path
|
return full_path
|
||||||
def dfs(self,sn):
|
def dfs(self,sn):
|
||||||
|
|
||||||
sn.dp = sn.val
|
sn.dp = sn.val
|
||||||
sn.final_pos = sn.pos
|
sn.final_pos = sn.pos
|
||||||
sn.path= [sn.pos]
|
sn.path= [sn]
|
||||||
cur = None
|
cur = None
|
||||||
for idx,child in enumerate(sn.children):
|
for idx,child in enumerate(sn.children):
|
||||||
self.dfs(child)
|
self.dfs(child)
|
||||||
if child.dp > 0:
|
if child.dp > 0:
|
||||||
sn.dp += child.dp
|
sn.dp += child.dp
|
||||||
|
|
||||||
if cur != None:
|
if cur != None:
|
||||||
sn.path.extend(self.getlca(cur,child))
|
sn.path.extend(self.getlca(sn.path[-1],child))
|
||||||
|
|
||||||
sn.path.extend(child.path)
|
sn.path.extend(child.path)
|
||||||
cur = child
|
cur = child
|
||||||
if idx == len(sn.children)-1:
|
sn.final_pos = cur.final_pos
|
||||||
sn.final_pos = cur.final_pos
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_path(self):
|
def get_path(self):
|
||||||
return self.path
|
return self.path
|
||||||
|
|
||||||
|
def bfs_path(self, start, end):
|
||||||
|
"""从start到end的最短路径(含首尾)"""
|
||||||
|
from collections import deque
|
||||||
|
n, m = self.rowNums, self.colNums
|
||||||
|
visited = [[False]*m for _ in range(n)]
|
||||||
|
prev = [[None]*m for _ in range(n)]
|
||||||
|
q = deque([start])
|
||||||
|
visited[start[0]][start[1]] = True
|
||||||
|
dx = [-1, 0, 1, 0]
|
||||||
|
dy = [0, -1, 0, 1]
|
||||||
|
while q:
|
||||||
|
x, y = q.popleft()
|
||||||
|
if (x, y) == end:
|
||||||
|
break
|
||||||
|
for i in range(4):
|
||||||
|
nx, ny = x + dx[i], y + dy[i]
|
||||||
|
if 0 <= nx < n and 0 <= ny < m and not visited[nx][ny]:
|
||||||
|
if self.maze[nx][ny] != '1':
|
||||||
|
visited[nx][ny] = True
|
||||||
|
prev[nx][ny] = (x, y)
|
||||||
|
q.append((nx, ny))
|
||||||
|
# 回溯路径
|
||||||
|
path = []
|
||||||
|
cur = end
|
||||||
|
while cur and cur != start:
|
||||||
|
path.append(cur)
|
||||||
|
cur = prev[cur[0]][cur[1]]
|
||||||
|
if cur == start:
|
||||||
|
path.append(start)
|
||||||
|
path.reverse()
|
||||||
|
return path
|
||||||
|
return []
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
sn = self.build_a_tree()
|
sn = self.build_a_tree()
|
||||||
# self.dfs_show(sn)
|
# self.dfs_show(sn)
|
||||||
self.dfs(sn)
|
self.dfs(sn)
|
||||||
self.path = sn.path
|
|
||||||
|
|
||||||
|
self.path =[_.pos for _ in sn.path]
|
||||||
|
# 如果终点不是e,则bfs补全
|
||||||
|
if self.path and self.end_pos and self.path[-1] != self.end_pos:
|
||||||
|
bfs_tail = self.bfs_path(self.path[-1], self.end_pos)
|
||||||
|
if bfs_tail:
|
||||||
|
self.path.extend(bfs_tail[1:])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def output_list(self):
|
def output_list(self):
|
||||||
|
28
maze.csv
28
maze.csv
@ -1,16 +1,16 @@
|
|||||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,t5,1,1
|
||||||
1,0,1,1,1,l22,1,0,1,l27,1,0,0,t17,0,1
|
1,0,1,0,1,1,1,1,1,1,1,0,1,0,0,1
|
||||||
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
|
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
|
||||||
|
|
2
maze.py
2
maze.py
@ -27,8 +27,6 @@ class Maze:
|
|||||||
self.source_collector = SourceCollector(maze=self.generater.maze)
|
self.source_collector = SourceCollector(maze=self.generater.maze)
|
||||||
self.source_collector.run()
|
self.source_collector.run()
|
||||||
self.full_path = self.source_collector.get_path()
|
self.full_path = self.source_collector.get_path()
|
||||||
for i in self.full_path:
|
|
||||||
print(i)
|
|
||||||
self.path_step = 0
|
self.path_step = 0
|
||||||
self.is_path_complete = False
|
self.is_path_complete = False
|
||||||
self.grid = self.generater.maze # 使用原始迷宫数据
|
self.grid = self.generater.maze # 使用原始迷宫数据
|
||||||
|
Loading…
Reference in New Issue
Block a user