From ff5d8893f2d919f6b1c7dca2daf034e55e9778e7 Mon Sep 17 00:00:00 2001 From: Gary Gan Date: Thu, 3 Jul 2025 20:44:29 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B4=AA=E5=BF=83=E7=AE=97=E6=B3=953.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tanxin.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 9 deletions(-) diff --git a/tanxin.py b/tanxin.py index a66d5cc..ada0f8e 100644 --- a/tanxin.py +++ b/tanxin.py @@ -52,6 +52,10 @@ class Greedy3x3ResourceCollector: # 预处理每个点到终点的距离 self.distance_to_end = self._calculate_distance_to_end() + + # 检查起点和终点的可达性 + if self.end not in self.distance_to_end: + print("警告:从起点无法到达终点!这可能是地图设计问题。") print(f"3x3视野贪心算法初始化") print(f"起始位置: {self.start}") @@ -305,7 +309,13 @@ class Greedy3x3ResourceCollector: elif self.can_move_to(path2_pos): return path2_pos, 0 - # 4. 优先级3:查找空地,优先选择离终点更近的 + # 4. 优先级3:检查是否有直接通往终点的路径 + for pos, cell_type, _ in adjacent_cells: + if pos == self.end: + print(f"发现终点 {pos},直接移动") + return pos, 0 + + # 5. 优先级4:查找空地,优先选择离终点更近的 empty_spaces = [(pos, self.distance_to_end.get(pos, float('inf'))) for pos, cell_type, _ in adjacent_cells if cell_type == 'empty'] @@ -426,7 +436,7 @@ class Greedy3x3ResourceCollector: def run_3x3_greedy_collection(self, max_moves=1000): """ 运行3x3视野贪心资源收集算法 - 严格按照优先级:金币 > 未走过的路 > 走过的路 > 墙/陷阱 + 严格按照优先级:金币 > 通往终点的路 > 未走过的路 > 走过的路 > 墙/陷阱 对于走过的路,优先走很久之前走过的路 Args: @@ -439,8 +449,12 @@ class Greedy3x3ResourceCollector: moves = 0 stuck_count = 0 # 连续无法找到资源的次数 - max_stuck = 20 # 最大连续无资源次数 - + max_stuck = 100 # 增加最大连续无资源次数,确保有足够的探索空间 + + # 初始检查:确保终点是可达的 + if self.end not in self.distance_to_end: + print("警告:从起点无法到达终点!") + while moves < max_moves and stuck_count < max_stuck: moves += 1 @@ -460,15 +474,32 @@ class Greedy3x3ResourceCollector: stuck_count = 0 # 收集到资源后重置无资源计数 else: # 是普通路径 - if best_pos not in self.explored_positions: + if best_pos == self.end: + print(f"第{moves}步: 移动到终点 位置{best_pos}") + stuck_count = 0 # 移动到终点也重置无资源计数 + elif best_pos not in self.explored_positions: print(f"第{moves}步: 移动到未走过的路 位置{best_pos}") + stuck_count += 1 else: print(f"第{moves}步: 移动到走过的路 位置{best_pos}") - stuck_count += 1 + if abs(best_pos[0] - self.end[0]) + abs(best_pos[1] - self.end[1]) <= 2: + # 靠近终点,减轻卡住计数 + stuck_count = max(0, stuck_count - 1) + else: + stuck_count += 1 else: - # 没有可移动位置,结束收集 - print(f"第{moves}步: 无法进行任何移动,结束收集") - break + # 尝试使用备选策略寻找可移动位置 + backup_pos = self.find_backup_move() + if backup_pos: + print(f"第{moves}步: 使用备选策略找到移动位置 {backup_pos}") + self.current_pos = backup_pos + self.path.append(backup_pos) + self.explored_positions.add(backup_pos) + stuck_count += 1 + else: + # 确实没有可移动位置,结束收集 + print(f"第{moves}步: 无法进行任何移动,结束收集") + break # 检查是否达到终点 if self.current_pos == self.end: @@ -598,8 +629,15 @@ class Greedy3x3ResourceCollector: """ 判断当前位置是否是死胡同 死胡同的定义:除了来路外,周围全是墙/陷阱/已走过的路 + + 特别地,如果当前位置是终点附近,或者有通往终点的路径,不视为死胡同 """ x, y = pos + + # 检查是否在终点附近 + if abs(x - self.end[0]) + abs(y - self.end[1]) <= 2: + return False + valid_directions = 0 for dx, dy in [(0, 1), (1, 0), (0, -1), (-1, 0)]: @@ -609,6 +647,10 @@ class Greedy3x3ResourceCollector: (nx, ny) not in self.explored_positions): valid_directions += 1 + # 如果相邻位置是终点,绝对不是死胡同 + if (nx, ny) == self.end: + return False + # 如果没有未探索的方向,则是死胡同 return valid_directions == 0