显示回退路径

This commit is contained in:
Guanforever 2025-06-29 19:22:54 +08:00
parent 460256c1cb
commit 67d8e66e57
3 changed files with 66 additions and 25 deletions

View File

@ -119,7 +119,7 @@ class SourceCollector:
u_to_lca = []
node = u
while node != lca:
u_to_lca.append(node.pos)
u_to_lca.append(node)
node = node.fa
lca_to_v = []
@ -131,37 +131,80 @@ class SourceCollector:
node_list.append(lca)
node_list.reverse()
for node in node_list:
lca_to_v.append(node.pos)
full_path = u_to_lca + lca_to_v
lca_to_v.append(node)
full_path = u_to_lca + lca_to_v[:-1]
return full_path
def dfs(self,sn):
sn.dp = sn.val
sn.final_pos = sn.pos
sn.path= [sn.pos]
sn.path= [sn]
cur = None
for idx,child in enumerate(sn.children):
self.dfs(child)
if child.dp > 0:
sn.dp += child.dp
if cur != None:
sn.path.extend(self.getlca(cur,child))
sn.path.extend(self.getlca(sn.path[-1],child))
sn.path.extend(child.path)
cur = child
if idx == len(sn.children)-1:
sn.final_pos = cur.final_pos
sn.final_pos = cur.final_pos
def get_path(self):
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):
sn = self.build_a_tree()
# self.dfs_show(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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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 1
2 1 0 t15 0 1 0 0 1 0 0 1 0 l16 0 0 0 0 t8 0 0 0 1
3 1 1 0 1 1 0 1 1 0 1 1 1 1 0 1 1 1 1 0 1
4 1 0 0 1 0 1 0 0 1 t9 0 1 0 0 1 0 1 l15 0 0 1
5 1 0 1 1 0 1 1 t18 l15 1 1 0 1 0 1 0 1 1 0 1 1
6 1 b50 0 0 1 0 0 1 0 t15 g11 0 0 1 0 1 0 l16 l26 0 1 1
7 1 1 g11 0 1 1 0 1 1 0 1 1 t19 1 0 1 0 1 0 1 1
8 1 0 t20 1 0 0 1 0 0 1 0 1 0 0 1 0 1 0 l11 1 0 1
9 1 1 0 1 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 1 1
10 1 0 1 g30 0 0 1 0 1 0 1 l19 0 1 0 0 1 0 0 1 0 1
11 1 0 1 1 0 1 1 0 1 l20 1 g19 1 1 0 t8 1 1 0 1 1 l12 1 1 1
12 1 g11 0 e 0 0 0 1 0 s 1 0 g13 0 1 0 0 1 0 0 1 0 0 0 b70 1
13 1 0 1 1 0 0 1 0 1 1 1 0 1 1 1 1 1 s 1 1
14 1 0 1 0 0 1 0 0 0 1 0 0 e 1 0 0 t8 1 g13 0 t5 0 1 1
15 1 0 1 1 0 1 l22 1 1 0 1 1 l27 1 1 0 0 1 t17 0 0 1
16 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

View File

@ -27,8 +27,6 @@ class Maze:
self.source_collector = SourceCollector(maze=self.generater.maze)
self.source_collector.run()
self.full_path = self.source_collector.get_path()
for i in self.full_path:
print(i)
self.path_step = 0
self.is_path_complete = False
self.grid = self.generater.maze # 使用原始迷宫数据