Merge branch 'main' of git.gangary.cn:gary/maze_python

This commit is contained in:
Gary Gan 2025-06-30 14:43:53 +08:00
commit 10ab7061ac
2 changed files with 6 additions and 16 deletions

View File

@ -138,23 +138,14 @@ class SourceCollector:
sn.dp = sn.val sn.dp = sn.val
sn.final_pos = sn.pos sn.final_pos = sn.pos
sn.path = [sn] sn.path = [sn]
# 对子节点按收益/距离优先遍历 # 先对子节点全部深搜一遍,收集路径长度
children = sn.children[:] children = sn.children[:]
# 计算每个child的“贪心优先级”金币优先距离近优先 for child in children:
def child_priority(child): self.dfs(child)
# 距离=曼哈顿距离
dist = abs(child.pos[0] - sn.pos[0]) + abs(child.pos[1] - sn.pos[1]) children.sort(key=lambda c: len(c.path))
# 金币优先,陷阱次之
if self.maze[child.pos[0]][child.pos[1]].startswith('g'):
return (0, dist) # 金币优先,距离近优先
elif self.maze[child.pos[0]][child.pos[1]].startswith('t'):
return (2, dist) # 陷阱最后
else:
return (1, dist) # 普通通路
children.sort(key=child_priority)
cur = None cur = None
for idx, child in enumerate(children): for idx, child in enumerate(children):
self.dfs(child)
if child.dp > 0: if child.dp > 0:
sn.dp += child.dp sn.dp += child.dp
if cur is not None: if cur is not None:

View File

@ -378,9 +378,8 @@ def main():
print("\n读取的迷宫:") print("\n读取的迷宫:")
reader.print_maze() reader.print_maze()
for i in generator.get_history_mazes(): for i in generator.get_history_mazes():
for j in i : for j in i:
print(j) print(j)
print() print()
if __name__ == "__main__": if __name__ == "__main__":