Python解決走迷宮問題算法示例
本文實例講述了Python解決走迷宮問題算法。分享給大家供大家參考,具體如下:
問題:
輸入n * m 的二維數(shù)組 表示一個迷宮
數(shù)字0表示障礙 1表示能通行
移動到相鄰單元格用1步
思路:
深度優(yōu)先遍歷,到達每一個點,記錄從起點到達每一個點的最短步數(shù)
初始化案例:
1 1 0 1 1
1 0 1 1 1
1 0 1 0 0
1 0 1 1 1
1 1 1 0 1
1 1 1 1 1
1 把圖周圍加上一圈-1 , 在深度優(yōu)先遍歷的時候防止出界
2 把所有障礙改成-1,把能走的地方改成0
3 每次遍歷經(jīng)歷某個點的時候,如果當前節(jié)點值是0 把花費的步數(shù)存到節(jié)點里
如果當前節(jié)點值是-1 代表是障礙 不遍歷它
如果走到當前節(jié)點花費的步數(shù)比里面存的小,就修改它
修改后的圖:
-1 -1 -1 -1 -1 -1 -1
-1 0 0 -1 0 0 -1
-1 0 -1 0 0 0 -1
-1 0 -1 0 -1 -1 -1
-1 0 -1 0 0 0 -1
-1 0 0 0 -1 0 -1
-1 0 0 0 0 0 -1
-1 -1 -1 -1 -1 -1 -1
外周的-1 是遍歷的時候防止出界的
默認從左上角的點是入口 右上角的點是出口
Python代碼:
# -*- coding:utf-8 -*-
def init():
global graph
graph.append([-1, -1, -1, -1, -1, -1, -1])
graph.append([-1, 0, 0, -1, 0, 0, -1])
graph.append([-1, 0, -1, 0, 0, 0, -1])
graph.append([-1, 0, -1, 0, -1, -1, -1])
graph.append([-1, 0, -1, 0, 0, 0, -1])
graph.append([-1, 0, 0, 0, -1, 0, -1])
graph.append([-1, 0, 0, 0, 0, 0, -1])
graph.append([-1, -1, -1, -1, -1, -1, -1])
#深度優(yōu)先遍歷
def deepFirstSearch( steps , x, y ):
global graph
current_step = steps + 1
print(x, y, current_step )
graph[x][y] = current_step
next_step = current_step + 1
'''
遍歷周圍4個點:
如果周圍節(jié)點不是-1 說明 不是障礙 在此基礎(chǔ)上:
里面是0 說明沒遍歷過 我們把它修改成當前所在位置步數(shù)加1
里面比當前的next_step大 說明不是最優(yōu)方案 就修改它
里面比當前next_step說明當前不是最優(yōu)方案,不修改
'''
if not(x-1== 1 and y==1) and graph[x-1][y] != -1 and ( graph[x-1][y]>next_step or graph[x-1][y] ==0 ) : #左
deepFirstSearch(current_step, x-1 , y )
if not(x == 1 and y-1==1) and graph[x][y-1] != -1 and ( graph[x][y-1]>next_step or graph[x][y-1] ==0 ) : #上
deepFirstSearch(current_step, x , y-1 )
if not(x == 1 and y+1==1) and graph[x][y+1] != -1 and ( graph[x][y+1]>next_step or graph[x][y+1]==0 ) : #下
deepFirstSearch(current_step, x , y+1 )
if not(x+1== 1 and y==1) and graph[x+1][y] != -1 and ( graph[x+1][y]>next_step or graph[x+1][y]==0 ) : #右
deepFirstSearch(current_step, x+1 , y )
if __name__ == "__main__":
graph = []
init()
deepFirstSearch(-1,1,1)
print(graph[1][5])
運行結(jié)果:
(1, 1, 0)
(1, 2, 1)
(2, 1, 1)
(3, 1, 2)
(4, 1, 3)
(5, 1, 4)
(5, 2, 5)
(5, 3, 6)
(4, 3, 7)
(3, 3, 8)
(2, 3, 9)
(2, 4, 10)
(1, 4, 11)
(1, 5, 12)
(2, 5, 13)
(2, 5, 11)
(4, 4, 8)
(4, 5, 9)
(5, 5, 10)
(6, 5, 11)
(6, 4, 12)
(6, 3, 13)
(6, 2, 14)
(6, 1, 15)
(6, 3, 7)
(6, 2, 8)
(6, 1, 9)
(6, 4, 8)
(6, 5, 9)
(6, 2, 6)
(6, 1, 7)
(6, 1, 5)
12
PS:本站還有一個無限迷宮游戲,基于JS實現(xiàn),提供給大家參考一下:
在線迷宮小游戲:
http://tools.jb51.net/games/migong
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python編碼操作技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進階經(jīng)典教程》
希望本文所述對大家Python程序設計有所幫助。
相關(guān)文章
PyQt5中QCommandLinkButton的詳細教程與應用實戰(zhàn)
在PyQt5中,QCommandLinkButton是一個特殊的按鈕控件,它最初在Windows Vista中引入,并因其獨特的外觀和功能在GUI應用程序中得到了廣泛應用,本教程將結(jié)合實際案例,詳細介紹QCommandLinkButton在PyQt5中的用法,需要的朋友可以參考下2024-07-07
wxpython 最小化到托盤與歡迎圖片的實現(xiàn)方法
這篇文章主要分享一個python實例代碼,使用wxpython實現(xiàn)最小化到托盤與歡迎圖片,需要的朋友可以參考下2014-06-06
Python數(shù)據(jù)類型之Tuple元組實例詳解
這篇文章主要介紹了Python數(shù)據(jù)類型之Tuple元組,結(jié)合實例形式分析了Python元組類型的概念、定義、讀取、連接、判斷等常見操作技巧與相關(guān)注意事項,需要的朋友可以參考下2019-05-05

