python游戲地圖最短路徑求解
一.題目要求
參考下圖完成游戲地圖中從起點(diǎn)到目標(biāo)點(diǎn)的最短路徑尋找問題。

二.設(shè)計(jì)思路
先對(duì)游戲地圖做了幾個(gè)設(shè)定,以矩陣來模擬游戲地圖。將可行的區(qū)域位置賦值0,障礙區(qū)賦值為inf。考慮到地圖大小,將起始點(diǎn)和終點(diǎn)區(qū)域賦值99。
從Start點(diǎn)A開始向外層擴(kuò)展,每擴(kuò)展一層pathlen加一。List Q存儲(chǔ)當(dāng)前需要擴(kuò)展的點(diǎn),list P 存儲(chǔ)當(dāng)前擴(kuò)展層。當(dāng)擴(kuò)展到End點(diǎn)B時(shí)擴(kuò)展結(jié)束,路徑可規(guī)劃。當(dāng)Q為空時(shí),本次層擴(kuò)展結(jié)束,檢查P,若P非空,從P層向外擴(kuò)展,若P為空,則End點(diǎn)B無法到達(dá)。
尋找最短路徑時(shí),從End點(diǎn)B開始,尋找當(dāng)前點(diǎn)附近8個(gè)點(diǎn)的標(biāo)記中比當(dāng)前點(diǎn)標(biāo)記小的點(diǎn),直到標(biāo)記為1為止。
三.程序主體
# -*-coding:gbk -*-
from numpy import *
dirs = [(1,1),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1)] # 四鄰位置:從右下角開始順時(shí)針得到,是按坐標(biāo)差得到的
def find_path(oldmap,A,B):
oldmap[A[0], A[1]] = 99
oldmap[B[0], B[1]] = 99
[a,b]=oldmap.shape
pathmap=oldmap.copy()
Q=[]#存儲(chǔ)擴(kuò)展節(jié)點(diǎn)
P=[]#往外一層
pathlen=1
if A==B:
print('start point is equal to end point')
return True
current=A
while (True):
for i in range(8):
neighbor=[current[0]+dirs[i][0], current[1]+dirs[i][1]]
if neighbor==B:
print('the way is found')######################wrong
print('中間過程')
print(oldmap)
find_way(oldmap,pathmap,A,B,a,b)#####調(diào)用路徑函數(shù)
return True
if (neighbor[0]>=0 and neighbor[1]>=0 and neighbor[0]<a and neighbor[1]<b and oldmap[neighbor[0],neighbor[1]]==0):
P.append(neighbor)
oldmap[neighbor[0],neighbor[1]]=pathlen
if Q==[]:
if P ==[]:
print(oldmap) ##############
print('No path')
return False
else:
Q.extend(P)
P=[]
pathlen += 1
else:
current=Q.pop()
###################尋找最短路徑
def find_way(oldmap,pathmap,A,B,a,b):
currentpos=B
while (oldmap[currentpos[0],currentpos[1]]!=1):
for i in range(8):
neighborpos=[currentpos[0]+dirs[i][0], currentpos[1]+dirs[i][1]]
if (neighborpos[0] >= 0 and neighborpos[1] >= 0 and neighborpos[0] < a and neighborpos[1] < b and oldmap[neighborpos[0],neighborpos[1]]!=0):
if oldmap[neighborpos[0],neighborpos[1]]<oldmap[currentpos[0],currentpos[1]]:
pathmap[neighborpos[0],neighborpos[1]]=oldmap[neighborpos[0],neighborpos[1]]
currentpos=neighborpos
break
print('the way:')
print(pathmap)
四.主函數(shù)
def main():
map =mat([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, inf,inf, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0,inf, 0, 0, 0, 0, 0, 0, 0],
[inf,inf,inf, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0,inf, 0, 0, 0, 0, 0, 0, 0, 0, 0, inf],
[0, 0,inf, 0, 0, 0, 0, 0, 0, 0, 0, 0, inf],
[0, 0,inf, 0, 0, 0, 0, 0, 0, 0, 0, 0,inf],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, inf],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],])
print('最初地圖')
print(map)
print('**********************************')
A = [5, 0]
# B=[5,0]
B = [3, 12]
find_path(map,A, B)
if __name__=='__main__':
main()
五.運(yùn)行結(jié)果


六.結(jié)果分析
由中間過程對(duì)應(yīng)的矩陣可知,共經(jīng)歷了12次向外層擴(kuò)展,第12次擴(kuò)展即可將目標(biāo)點(diǎn)包含進(jìn)去。最短路徑如the way對(duì)應(yīng)的矩陣所示,是通過一種類似梯度下降的方法得到的。
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
使用Pycharm創(chuàng)建一個(gè)Django項(xiàng)目的超詳細(xì)圖文教程
Django是比較經(jīng)典的Python web框架,最近剛好在項(xiàng)目中用到了Django,所以下面這篇文章主要給大家介紹了關(guān)于使用Pycharm創(chuàng)建一個(gè)Django項(xiàng)目的超詳細(xì)圖文教程,文中介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
使用Python快速生成chrome插件相關(guān)文件結(jié)構(gòu)
本文主要介紹了如何使用Python編寫一個(gè)程序,它允許用戶創(chuàng)建一些特定文件并將它們保存在指定的文件夾中,同時(shí)也能夠啟動(dòng)?Google?Chrome?瀏覽器并打開擴(kuò)展頁面,感興趣的可以了解一下2024-11-11
關(guān)于數(shù)據(jù)分析Pandas的Series用法總結(jié)
這篇文章主要介紹了關(guān)于數(shù)據(jù)分析Pandas的Series用法總結(jié),Series序列,是一種一維的結(jié)構(gòu),類似于一維列表和ndarray中的一維數(shù)組,但是功能比他們要更為強(qiáng)大,Series由兩部分組成:索引index和數(shù)值values,本篇對(duì)其用法做出總結(jié)2023-07-07
Python 列表(List) 的三種遍歷方法實(shí)例 詳解
這篇文章主要介紹了Python 列表(List) 的三種遍歷方法實(shí)例 詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

