一道python走迷宮算法題
前幾天逛博客時(shí)看到了這樣一道問題,感覺比較有趣,就自己思考了下方案順便用python實(shí)現(xiàn)了一下。題目如下:
用一個(gè)二維數(shù)組表示一個(gè)簡單的迷宮,用0表示通路,用1表示阻斷,老鼠在每個(gè)點(diǎn)上可以移動相鄰的東南西北四個(gè)點(diǎn),設(shè)計(jì)一個(gè)算法,模擬老鼠走迷宮,找到從入口到出口的一條路徑。
如圖所示:
先說下我的思路吧:
1、首先用一個(gè)列表source存儲迷宮圖,一個(gè)列表route_stack存儲路線圖,一個(gè)列表route_history存儲走過的點(diǎn),起點(diǎn)(0,0),終點(diǎn)(4,4)。
2、老鼠在每個(gè)點(diǎn)都有上下左右四種方案可選,需要定義這些方案的執(zhí)行方法。
3、最后做一個(gè)循環(huán),如果當(dāng)前點(diǎn)不是(4,4)的話就依次執(zhí)行上下左右四種方法,但是有些限制,比如嘗試走過的點(diǎn)不會再嘗試走,(0,x)點(diǎn)無法再執(zhí)行向上的方法等等。
貼一下代碼:
# _*_ coding:utf-8 _*_ route_stack = [[0,0]] route_history = [[0,0]] source=[[0,0,1,0,1],[1,0,0,0,1],[0,0,1,1,0],[0,1,0,0,0],[0,0,0,1,0]] def up(location): #橫坐標(biāo)為0,無法再向上走 if location[1] == 0: return False else: new_location = [location[0],location[1]-1] #已經(jīng)嘗試過的點(diǎn)不會嘗試第二次 if new_location in route_history: return False #碰到墻不走 elif source[new_location[0]][new_location[1]] == 1: return False else: route_stack.append(new_location) route_history.append(new_location) return True def down(location): if location[1] == 4: return False else: new_location = [location[0],location[1]+1] if new_location in route_history: return False elif source[new_location[0]][new_location[1]] == 1: return False else: route_stack.append(new_location) route_history.append(new_location) return True def left(location): if location[0] == 0: return False else: new_location = [location[0]-1,location[1]] if new_location in route_history: return False elif source[new_location[0]][new_location[1]] == 1: return False else: route_stack.append(new_location) route_history.append(new_location) return True def right(location): if location[0] == 4: return False else: new_location = [location[0]+1,location[1]] if new_location in route_history: return False elif source[new_location[0]][new_location[1]] == 1: return False else: route_stack.append(new_location) route_history.append(new_location) return True lo = [0,0] while route_stack[-1] != [4,4]: if up(lo): lo = route_stack[-1] continue if down(lo): lo = route_stack[-1] continue if left(lo): lo = route_stack[-1] continue if right(lo): lo = route_stack[-1] continue route_stack.pop() lo = route_stack[-1] print route_stack
執(zhí)行結(jié)果如下:
題目出處有另一種解題思路,但是我覺得有點(diǎn)煩,自己的這個(gè)比較好理解點(diǎn),實(shí)現(xiàn)起來也比較方便。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python3中configparser模塊讀寫ini文件并解析配置的用法詳解
這篇文章主要介紹了Python3中configparser模塊讀寫ini文件并解析配置的用法詳解,需要的朋友可以參考下2020-02-02Python+tkinter制作經(jīng)典登錄界面和點(diǎn)擊事件
Tkinter是?Python?標(biāo)準(zhǔn)?GUI?庫,簡稱?“Tk”;從本質(zhì)上來說,它是對?TCL/TK?工具包的一種?Python?接口封裝。本文將利用tkinter制作一個(gè)經(jīng)典的登錄界面和點(diǎn)擊事件,需要的可以參考一下2022-09-09Python3實(shí)現(xiàn)英文字母轉(zhuǎn)換哥特式字體實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Python3實(shí)現(xiàn)英文字母轉(zhuǎn)換哥特式字體的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09python數(shù)字圖像處理之估計(jì)噪聲參數(shù)
這篇文章主要介紹了python數(shù)字圖像處理之估計(jì)噪聲參數(shù),圖像復(fù)原與重建,想了解圖像處理的同學(xué),一定要好好看看2021-04-04Caffe卷積神經(jīng)網(wǎng)絡(luò)數(shù)據(jù)層及參數(shù)
這篇文章主要為大家介紹了Caffe卷積神經(jīng)網(wǎng)絡(luò)數(shù)據(jù)層及參數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06python爬取分析超級大樂透歷史開獎(jiǎng)數(shù)據(jù)
這篇文章主要介紹了python爬取分析超級大樂透歷史開獎(jiǎng)數(shù)據(jù),本次使用了requests和beautifulsoup庫進(jìn)行數(shù)據(jù)的爬取,通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02Python使用os.listdir()和os.walk()獲取文件路徑與文件下所有目錄的方法
今天小編就為大家分享一篇關(guān)于Python使用os.listdir()和os.walk()獲取文件路徑與文件下所有目錄的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04