这个关卡不能用“api.check.next()”这个函数。
# 依次计算场上所有宝石到企鹅的距离,将target_gem设置为距离更小的宝石名称 def target_choice(context): min_distance = 99 for i in range(0, 5): path_gem = api.check.path(start=(context.me.row, context.me.col), end=(context.items["red_gem"][i].row, context.items["red_gem"][i].col)) gem_distance = len(path_gem) - 1 if gem_distance < min_distance: min_distance = gem_distance target_gem = context.items["red_gem"][i] return target_gem def update(context): m_row = context.me.row m_col = context.me.col gem_row = target_choice(context).row gem_col = target_choice(context).col #计算当前企鹅到出口的有效路径距离 exit_row = context.exit.row exit_col = context.exit.col path_exit = api.check.path(start=(m_row, m_col), end=(exit_row, exit_col)) exit_distance = len(path_exit) m_energy = context.me.energy #如果当前企鹅体力小于等于出口距离,就走向出口 if m_energy <= exit_distance: t_row = path_exit[1][0] t_col = path_exit[1][1] #否则,就走向最近的宝石 else: path_gem = api.check.path(start=(m_row, m_col), end=(gem_row, gem_col)) t_row = path_gem[1][0] t_col = path_gem[1][1] #开始向目标移动 if t_row > m_row: return "D" elif t_row < m_row: return "U" elif t_col > m_col: return "R" elif t_col < m_col: return "L"