Compare commits
No commits in common. "ecb7cb59aa9a485411520baf84b4d9bca4cbc674" and "9dddc9363f5ec4b8b26e80f2ea6e418cbe659b03" have entirely different histories.
ecb7cb59aa
...
9dddc9363f
@ -138,14 +138,23 @@ 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[:]
|
||||||
for child in children:
|
# 计算每个child的“贪心优先级”:金币优先,距离近优先
|
||||||
self.dfs(child)
|
def child_priority(child):
|
||||||
|
# 距离=曼哈顿距离
|
||||||
children.sort(key=lambda c: len(c.path))
|
dist = abs(child.pos[0] - sn.pos[0]) + abs(child.pos[1] - sn.pos[1])
|
||||||
|
# 金币优先,陷阱次之
|
||||||
|
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:
|
||||||
|
@ -380,6 +380,7 @@ def main():
|
|||||||
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