上一篇里面打漏了,现在发也全漏了,小发部分
import numpy as np import pandas as pd import matplotlib.pyplot as plt from deap import base, creator, tools from heapq import heappush, heappop # 读取Excel文件 data = pd.read_excel('/A题/附件.xlsx', sheet_name='Sheet2', header=None) grid = data.values # 定义起点和目标点 start = (np.where(grid == 'Start')[0][0], np.where(grid == 'Start')[1][0]) targets = [(np.where(grid == f'target{i}')[0][0], np.where(grid == f'target{i}')[1][0]) for i in range(1, 6)] # A*算法 def heuristic(a, b): return abs(a[0] - b[0]) + abs(a[1] - b[1]) def astar(grid, start, end): neighbors = [(0, 1), (1, 0), (0, -1), (-1, 0)] close_set = set() came_from = {} gscore = {start: 0} fscore = {start: heuristic(start, end)} open_heap = [] heappush(open_heap, (fscore[start], start)) while open_heap:21 6 47 27 64 8