一、简介
两点之间寻找最短路径,要考虑到存在障碍物遮挡和斜线移动的情况。
二、具体说明
说明可以参考下面的链接,对A*算法实现的描述。
三、具体实现
1、实现功能
2、寻路具体流程
3、关于F值
f = g + h
g表示当前移动到下一个点的消耗,平移为1,斜移动为 sqrt((x1-x2)**2 +(y1-y2)**2);
h表示当前移动到终点的消耗,不考虑斜移,不考虑障碍物
具体原理请查看下面的参考链接。
完整实现:
# -*- coding: utf-8 -*-
import os,sys,random,math
#地图设置
gameMapWidth = 10
gameMapHeight = 10
gameMap = []
#地图障碍物
obstacleCount = 5
#块状态
ITEM_STAT_NORMAL = 0 #空点
ITEM_STAT_OBSTACLE = 1 #障碍物
ITEM_STAT_START = 2 #起点
ITEM_STAT_END = 3 #终点
#起点和终点
spNum = -1
epNum = -1
#每块的属性
class Item:
def __init__(self,x,y,status):
self.x = x
self.y = y
self.status = status
self.mf = -1
self.mg = -1
self.mh = -1
self.mParent = None
self.isPath = 0
#初始化地图
def initMap():
for wc in xrange(gameMapWidth):
for hc in xrange(gameMapHeight):
gameMap.append(Item(wc,hc,ITEM_STAT_NORMAL))
#插入障碍物
for oc in xrange(obstacleCount):
choose = random.randint(gameMapWidth,gameMapWidth*gameMapHeight - 1)
gameMap[choose].status = ITEM_STAT_OBSTACLE
global spNum
global epNum
#选取起点和终点
while (spNum == -1):
choose = random.randint(0,gameMapWidth*gameMapHeight - 1)
if gameMap[choose].status == 0:
spNum = choose
gameMap[spNum].status = ITEM_STAT_START
while (epNum == -1):
choose = random.randint(0,gameMapWidth*gameMapHeight - 1)
if gameMap[choose].status == 0:
epNum = choose
gameMap[epNum].status = ITEM_STAT_END
#输出地图信息
def printMap():
for itemc in xrange(len(gameMap)):
if gameMap[itemc].status == ITEM_STAT_START:
print "START",
elif gameMap[itemc].status == ITEM_STAT_END:
print "END ",
elif gameMap[itemc].isPath == 1:
print "path ",
else:
print "%d " %(gameMap[itemc].status),
if (itemc + 1) % gameMapHeight == 0:
print "\n"
#寻路
def findPath():
global spNum
global epNum
#开启列表
openPointList = []
#关闭列表
closePointList = []
#开启列表插入起始点
openPointList.append(gameMap[spNum])
while (len(openPointList) > 0):
#寻找开启列表中最小预算值的点
minFPoint = findPointWithMinF(openPointList)
#从开启列表移除,添加到关闭列表
openPointList.remove(minFPoint)
closePointList.append(minFPoint)
#找到当前点周围点
surroundList = findSurroundPoint(minFPoint,closePointList)
#开始寻路
for sp in surroundList:
#存在在开启列表,说明上一块查找时并不是最优路径,考虑此次移动是否是最优路径
if sp in openPointList:
newPathG = CalcG(sp, minFPoint) #计算新路径下的G值
if newPathG < sp.mg:
sp.mg = newPathG
sp.mf = sp.mg + sp.mh
sp.mParent = minFPoint
else:
sp.mParent = minFPoint #当前查找到点指向上一个节点
CalcF(sp, gameMap[epNum])
openPointList.append(sp)
if gameMap[epNum] in openPointList:
gameMap[epNum].mParent = minFPoint
break
curp = gameMap[epNum]
while True:
curp.isPath = 1
curp = curp.mParent
if curp == None:
break
print "\n"
printMap()
def CalcG(point, minp):
return math.sqrt((point.x - point.mParent.x)**2 + (point.y - point.mParent.y)**2) + minp.mg
#计算每个点的F值
def CalcF(point,endp):
h = abs(endp.x - point.x) + abs(endp.y - point.y)
g = 0
if point.mParent == None:
g = 0
else:
g = point.mParent.mg + math.sqrt((point.x - point.mParent.x)**2 + (point.y - point.mParent.y)**2)
point.mg = g
point.mh = h
point.mf = g + h
return
#不能是障碍块,不包含在关闭列表中
def notObstacleAndClose(point,closePointList):
if point not in closePointList and point.status != ITEM_STAT_OBSTACLE:
return True
return False
#查找周围块
def findSurroundPoint(point,closePointList):
surroundList = []
up = None
down = None
left = None
right = None
leftUp = None
rightUp = None
leftDown = None
rightDown = None
#上面的点存在
if point.x > 0:
up = gameMap[gameMapHeight*(point.x - 1) + point.y]
if notObstacleAndClose(up,closePointList):
surroundList.append(up)
#下面的点存在
if point.x < gameMapWidth - 1:
down = gameMap[gameMapHeight*(point.x + 1)+ point.y]
if notObstacleAndClose(down,closePointList):
surroundList.append(down)
#左边的点存在
if point.y > 0:
left = gameMap[gameMapHeight*(point.x) + point.y - 1]
if notObstacleAndClose(left, closePointList):
surroundList.append(left)
#右边的点存在
if point.y < gameMapHeight - 1:
right = gameMap[gameMapHeight*(point.x) + point.y + 1]
if notObstacleAndClose(right,closePointList):
surroundList.append(right)
#斜方向的点还需考虑对应正方向不是障碍物
#左上角的点存在
if point.x > 0 and point.y > 0:
leftUp = gameMap[gameMapHeight*(point.x - 1) + point.y - 1]
if notObstacleAndClose(leftUp,closePointList) and left.status != ITEM_STAT_OBSTACLE and up.status != ITEM_STAT_OBSTACLE:
surroundList.append(leftUp)
#右上角的点存在
if point.x > 0 and point.y < gameMapHeight - 1:
rightUp = gameMap[gameMapHeight*(point.x-1) + point.y + 1]
if notObstacleAndClose(rightUp,closePointList) and right.status != ITEM_STAT_OBSTACLE and up.status != ITEM_STAT_OBSTACLE:
surroundList.append(rightUp)
#左下角的点存在
if point.x < gameMapWidth - 1 and point.y > 0:
leftDown = gameMap[gameMapHeight*(point.x+1) + point.y - 1]
if notObstacleAndClose(leftDown,closePointList) and left.status != ITEM_STAT_OBSTACLE and down.status != ITEM_STAT_OBSTACLE:
surroundList.append(leftDown)
#右下角的点存在
if point.x < gameMapWidth - 1 and point.y < gameMapHeight - 1:
rightDown = gameMap[gameMapHeight*(point.x+1) + point.y + 1]
if notObstacleAndClose(rightDown,closePointList) and right.status != ITEM_STAT_OBSTACLE and down.status != ITEM_STAT_OBSTACLE:
surroundList.append(rightDown)
return surroundList
#查找list中最小的f值
def findPointWithMinF(openPointList):
f = 0xffffff
temp = None
for pc in openPointList:
if pc.mf < f:
temp = pc
f = pc.mf
return temp
def main():
initMap() ##初始化地图
printMap() ##输出初始化地图信息
findPath() ##查找最优路径
#入口
main()
参考:
https://www.gamedev.net/articles/programming/artificial-intelligence/a-pathfinding-for-beginners-r2003/
https://blog.csdn.net/qq_33747722/article/details/78436919
文章浏览阅读1k次。1、HTML5 Web SQL 数据库 Web SQL 数据库 API 并不是 HTML5 规范的一部分,但是它是一个独立的规范,引入了一组使用 SQL 操作客户端数据库的 APIs。如果你是一个 Web 后端程序员,应该很容易理解 SQL 的操作。Web SQL 数据库可以在最新版的 Safari, Chrome 和 Opera 浏览器中工作。2、核心方法 以下是规范中定义的三个_方式准则的定义
文章浏览阅读4.1k次,点赞2次,收藏6次。spring Boot 中使用线程池异步执行多个定时任务在启动类中添加注解@EnableScheduling配置自定义线程池在启动类中添加注解@EnableScheduling第一步添加注解,这样才会使定时任务启动配置自定义线程池@Configurationpublic class ScheduleConfiguration implements SchedulingConfigurer..._springboot启动后自动开启多个线程程序
文章浏览阅读1.1k次。在项目的target文件夹下把之前"mvn clean package"生成的压缩包(我的是jar包)删掉重新执行"mvn clean package"再执行"mvn clean install"即可_mvn clean install有errors
文章浏览阅读974次。Navicat连接mysql数据库时,不断报1405错误,下面是针对这个的解决办法:MySQL服务器正在运行,停止它。如果是作为Windows服务运行的服务器,进入计算机管理--->服务和应用程序------>服务。如果服务器不是作为服务而运行的,可能需要使用任务管理器来强制停止它。创建1个文本文件(此处命名为mysql-init.txt),并将下述命令置于单一行中:SET PASSW..._nvarchar链接不上数据库
文章浏览阅读2.2k次。Python的requests模块是一个常用的HTTP库,用于发送HTTP请求和处理响应。_python requests 参数
文章浏览阅读2.7w次,点赞7次,收藏50次。APT攻击APT攻击是近几年来出现的一种高级攻击,具有难检测、持续时间长和攻击目标明确等特征。本文中,整理了近年来比较典型的几个APT攻击,并其攻击过程做了分析(为了加深自己对APT攻击的理解和学习)Google极光攻击2010年的Google Aurora(极光)攻击是一个十分著名的APT攻击。Google的一名雇员点击即时消息中的一条恶意链接,引发了一系列事件导致这个搜_2010谷歌网络被极光黑客攻击
文章浏览阅读1.1k次。JS代码 /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { setTimeout( function(){ wx.showToast({ title: '黄菊华老师', }) },2000 ) },说明该代码只执行一次..._微信小程序 settimeout 向上层传值
文章浏览阅读48次。uploadify2.1.4如何能使按钮显示中文博客分类:uploadify网上关于这段话的搜索恐怕是太多了。方法多也试过了不知怎么,反正不行。最终自己想办法给解决了。当然首先还是要有fla源码。直接去管网就可以下载。[url]http://www.uploadify.com/wp-content/uploads/uploadify-v2.1.4...
文章浏览阅读9.6k次,点赞5次,收藏36次。戴尔服务器安装VMware ESXI6.7.0教程(U盘安装)一、前期准备1、下载镜像下载esxi6.7镜像:VMware-VMvisor-Installer-6.7.0-8169922.x86_64.iso这里推荐到戴尔官网下载,Baidu搜索“戴尔驱动下载”,选择进入官网,根据提示输入服务器型号搜索适用于该型号服务器的所有驱动下一步选择具体类型的驱动选择一项下载即可待下载完成后打开软碟通(UItraISO),在“文件”选项中打开刚才下载好的镜像文件然后选择启动_vmware-vcsa-all-6.7.0-8169922.iso
文章浏览阅读2k次。百度语音技术永久免费的语音自动转字幕介绍基于百度语音技术,识别率97%无时长限制,无文件大小限制永久免费,简单,易用,速度快支持中文,英文,粤语永久免费的语音转字幕网站: http://thinktothings.com视频介绍 https://www.bilibili.com/video/av42750807 ...
文章浏览阅读7.6k次,点赞2次,收藏9次。Instrumentation是一种直接修改程序二进制文件的方法。其可以用于程序的调试,优化,安全等等。对这个词一般的翻译是“插桩”,但这更多使用于软件测试领域。【找一些相关的例子】Dyninst可以动态或静态的修改程序的二进制代码。动态修改是在目标进程运行时插入代码(dynamic binary instrumentation)。静态修改则是直接向二进制文件插入代码(static b_dyninst
文章浏览阅读2.9k次。部署asp网站到云服务器 内容精选换一换通常情况下,需要结合客户的实际业务环境和具体需求进行业务改造评估,建议您进行服务咨询。这里仅描述一些通用的策略供您参考,主要分如下几方面进行考虑:业务迁移不管您的业务是否已经上线华为云,业务迁移的策略是一致的。建议您将时延敏感型,有快速批量就近部署需求的业务迁移至IEC;保留数据量大,且需要长期稳定运行的业务在中心云上。迁移方法请参见如何计算隔离独享计算资源..._nas asp网站