diff --git a/SourceCollector.py b/SourceCollector.py index 7dc58e3..d795172 100644 --- a/SourceCollector.py +++ b/SourceCollector.py @@ -88,10 +88,8 @@ class SourceCollector: parent.children.append(new_node) queue.append((nx, ny, new_node)) return root - - - + def outofmap(self,x,y): return x < 0 or y < 0 or x > self.rowNums or y > self.colNums def getlca(self,u, v): @@ -136,20 +134,31 @@ class SourceCollector: full_path = u_to_lca + lca_to_v[:-1] return full_path - def dfs(self,sn): - + def dfs(self, sn): sn.dp = sn.val sn.final_pos = sn.pos - sn.path= [sn] + sn.path = [sn] + # 对子节点按收益/距离优先遍历 + children = sn.children[:] + # 计算每个child的“贪心优先级”:金币优先,距离近优先 + def child_priority(child): + # 距离=曼哈顿距离 + 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 - for idx,child in enumerate(sn.children): + for idx, child in enumerate(children): self.dfs(child) - if child.dp > 0: - sn.dp += child.dp - - if cur != None: - sn.path.extend(self.getlca(sn.path[-1],child)) - + if child.dp > 0: + sn.dp += child.dp + if cur is not None: + sn.path.extend(self.getlca(sn.path[-1], child)) sn.path.extend(child.path) cur = child sn.final_pos = cur.final_pos @@ -199,7 +208,11 @@ class SourceCollector: self.path =[_.pos for _ in sn.path] - # 如果终点不是e,则bfs补全 + for idx,item in enumerate(self.path): + if idx > 0: + if item == self.path[idx-1]: + del self.path[idx] + 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: diff --git a/maze.csv b/maze.csv deleted file mode 100644 index 81cd7cc..0000000 --- a/maze.csv +++ /dev/null @@ -1,16 +0,0 @@ -1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 -1,t15,1,0,1,0,1,l16,0,0,0,t8,0,0,0,1 -1,0,1,0,1,0,1,1,1,0,1,1,1,1,0,1 -1,0,1,0,1,0,1,0,0,0,1,0,l15,0,0,1 -1,0,1,0,1,t18,1,0,1,0,1,1,1,0,1,1 -1,0,1,0,1,t15,0,0,1,0,1,0,l26,0,1,1 -1,g11,1,0,1,0,1,t19,1,0,1,0,1,0,1,1 -1,0,1,0,1,0,1,0,1,0,1,0,1,l11,0,1 -1,0,1,0,1,0,1,1,1,0,1,1,1,1,1,1 -1,0,g30,0,1,0,1,0,l19,0,0,0,0,0,0,1 -1,1,0,1,1,1,1,1,0,1,1,1,1,1,1,1 -1,0,0,0,1,s,0,0,0,0,0,0,0,0,b70,1 -1,0,1,0,0,0,1,1,1,1,1,1,1,1,1,1 -1,0,1,0,1,0,0,0,0,e,0,t8,g13,t5,1,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