python游戲地圖最短路徑求解
一.題目要求
參考下圖完成游戲地圖中從起點到目標(biāo)點的最短路徑尋找問題。
二.設(shè)計思路
先對游戲地圖做了幾個設(shè)定,以矩陣來模擬游戲地圖。將可行的區(qū)域位置賦值0,障礙區(qū)賦值為inf??紤]到地圖大小,將起始點和終點區(qū)域賦值99。
從Start點A開始向外層擴(kuò)展,每擴(kuò)展一層pathlen加一。List Q存儲當(dāng)前需要擴(kuò)展的點,list P 存儲當(dāng)前擴(kuò)展層。當(dāng)擴(kuò)展到End點B時擴(kuò)展結(jié)束,路徑可規(guī)劃。當(dāng)Q為空時,本次層擴(kuò)展結(jié)束,檢查P,若P非空,從P層向外擴(kuò)展,若P為空,則End點B無法到達(dá)。
尋找最短路徑時,從End點B開始,尋找當(dāng)前點附近8個點的標(biāo)記中比當(dāng)前點標(biāo)記小的點,直到標(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)] # 四鄰位置:從右下角開始順時針得到,是按坐標(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=[]#存儲擴(kuò)展節(jié)點 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()
五.運行結(jié)果
六.結(jié)果分析
由中間過程對應(yīng)的矩陣可知,共經(jīng)歷了12次向外層擴(kuò)展,第12次擴(kuò)展即可將目標(biāo)點包含進(jìn)去。最短路徑如the way對應(yīng)的矩陣所示,是通過一種類似梯度下降的方法得到的。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django超詳細(xì)講解圖書管理系統(tǒng)的實現(xiàn)
前段時間翻文件發(fā)現(xiàn)了以前學(xué)習(xí)python和django時做的一個系統(tǒng),當(dāng)時的想法是將這玩意做出來應(yīng)付web開發(fā)大作業(yè)、課程設(shè)計作業(yè)甚至是畢設(shè)用的,實際上也確實應(yīng)付了課程設(shè)計,功能雖然不算多,但是應(yīng)付課程設(shè)計或者大作業(yè)綽綽有余了2022-07-07Django基礎(chǔ)知識與基本應(yīng)用入門教程
這篇文章主要介紹了Django基礎(chǔ)知識與基本應(yīng)用,結(jié)合實例形式分析了Django框架基本的項目創(chuàng)建、啟動、查看版本等操作,并結(jié)合一個簡單的blog應(yīng)用示例分析了Django的基本使用方法,需要的朋友可以參考下2018-07-07