补丁 : 修正了dp路径长度不优bug
This commit is contained in:
parent
412813db95
commit
cdb3700251
@ -138,24 +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))
|
||||||
# 金币优先,陷阱次之
|
|
||||||
print(dist)
|
|
||||||
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:
|
||||||
|
@ -374,9 +374,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__":
|
||||||
|
Loading…
Reference in New Issue
Block a user