用二維列表模擬迷宮李根,1代表墻槽奕,0代表當前路是可以通過的
回溯法的核心是狀態(tài)的轉(zhuǎn)換房轿,當當前狀態(tài)不能進入下一狀態(tài),我們就回溯到之前能進入下一狀態(tài)的某狀態(tài)結(jié)點夯接,我們用棧的append和pop去模擬這一過程
# 起始位置為(1, 1) 終點位置為(8, 8)
maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 1, 1, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1, 1, 0, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
首先我們定義一個函數(shù)纷妆,函數(shù)的參數(shù)分別為起始位置
和終點位置
def path(x1, y1, x2, y2):
pass
path(1, 1, 8, 8)
我們定義當前狀態(tài)位置向四個方向轉(zhuǎn)移的函數(shù)
, 用python的λ函數(shù)
來實習(xí)
directions = [
lambda x, y: (x - 1, y), # 上
lambda x, y: (x + 1, y), # 下
lambda x, y: (x, y - 1), # 左
lambda x, y: (x, y + 1), # 右
]
我們寫一個最簡易的框架, 定義一個棧 把初始位置的元組放入棧,當棧不為空時候逊拍,不停尋找下一狀態(tài),當棧為空時际邻,說明當前迷宮是死路
,我們設(shè)置終止狀態(tài)
def path(x1, y1, x2, y2):
stack = []
stack.append((x1, y1))
while len(stack) > 0:
# 當前狀態(tài)
cur_node = stack[-1]
# 終止狀態(tài)
if cur_node[0] == x2 and cur_node[1] == y2:
return True
pass
# 當棧為空時 說明為死路 走不到終點
else:
return False
接下來我們定義下一狀態(tài)next_node通過四個方向的函數(shù)不斷的判斷是否能通過 如果四個方向皆不能滿足條件則利用stack的pop操作回溯到之前的某一能使條件滿足的狀態(tài)注整, 為防止環(huán)路的出現(xiàn),每次訪問過的路徑都設(shè)置為已訪問狀態(tài)肿轨。
for direction in directions:
# 下一狀態(tài)
next_node = direction(cur_node[0], cur_node[1])
if maze[next_node[0]][next_node[1]] == 0:
stack.append(next_node)
maze[next_node[0]][next_node[1]] = 2 # 把訪問過的路徑置為已訪問
break # 每成功一次就立刻進入下一次狀態(tài)的尋找
# 如果四個方向皆不成立 回溯到之前的狀態(tài) 直到有條件滿足當前狀態(tài)
else:
# 利用棧的pop操作進行回溯
stack.pop()
maze[next_node[0]][next_node[1]] = 2
最終的代碼實現(xiàn)如下, 當滿足終止條件時蕊程,我們遍歷這個棧獲得詳細的路徑。
maze = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 1, 0, 0, 0, 1, 0, 1],
[1, 0, 0, 0, 0, 1, 1, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 1, 0, 0, 0, 0, 1],
[1, 0, 1, 0, 0, 0, 1, 0, 0, 1],
[1, 0, 1, 1, 1, 0, 1, 1, 0, 1],
[1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
directions = [
lambda x, y: (x - 1, y), # 上
lambda x, y: (x + 1, y), # 下
lambda x, y: (x, y - 1), # 左
lambda x, y: (x, y + 1), # 右
]
def path(x1, y1, x2, y2):
stack = []
stack.append((x1, y1))
while len(stack) > 0:
# 當前狀態(tài)
cur_node = stack[-1]
if cur_node[0] == x2 and cur_node[1] == y2:
for pat in stack:
print(pat)
return True
print(stack)
for direction in directions:
# 下一狀態(tài)
next_node = direction(cur_node[0], cur_node[1])
if maze[next_node[0]][next_node[1]] == 0:
stack.append(next_node)
maze[next_node[0]][next_node[1]] = 2 # 把訪問過的路徑置為已訪問
break # 每成功一次就立刻進入下一次狀態(tài)的尋找
# 如果四個方向皆不成立 回溯到之前的狀態(tài) 直到有條件滿足當前狀態(tài)
else:
# 利用棧的pop操作進行回溯
stack.pop()
maze[next_node[0]][next_node[1]] = 2
# 當棧為空時 說明為死路 走不到終點
else:
return False
print(path(1, 1, 8, 8))
某一可達到終點的路徑
(1, 1)
(2, 1)
(1, 1)
(1, 2)
(2, 2)
(3, 2)
(3, 1)
(4, 1)
(5, 1)
(5, 2)
(5, 3)
(6, 3)
(6, 4)
(6, 5)
(5, 5)
(4, 5)
(4, 6)
(5, 6)
(5, 7)
(4, 7)
(3, 7)
(3, 8)
(4, 8)
(5, 8)
(6, 8)
(7, 8)
(8, 8)
可以通過打印棧的增長和減小過程來感受不停尋找下一狀態(tài)和回溯的過程
print(stack)
[(1, 1)]
[(1, 1), (2, 1)]
[(1, 1), (2, 1), (1, 1)]
[(1, 1), (2, 1), (1, 1), (1, 2)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (6, 1)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (6, 1), (7, 1)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (6, 1)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7), (3, 8)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7), (3, 8), (2, 8)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7), (3, 8), (2, 8), (1, 8)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7), (3, 8), (2, 8)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7), (3, 8)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7), (3, 8), (4, 8)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7), (3, 8), (4, 8), (5, 8)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7), (3, 8), (4, 8), (5, 8), (6, 8)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7), (3, 8), (4, 8), (5, 8), (6, 8), (7, 8)]
[(1, 1), (2, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (4, 1), (5, 1), (5, 2), (5, 3), (6, 3), (6, 4), (6, 5), (5, 5), (4, 5), (4, 6), (5, 6), (5, 7), (4, 7), (3, 7), (3, 8), (4, 8), (5, 8), (6, 8), (7, 8), (8, 8)]