华数杯第四问

上一篇里面打漏了,现在发也全漏了,小发部分

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
  • 8
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值