Diff
checker
文本
文本
圖像
文檔
Excel
文件夾
Legal
Enterprise
桌面版
定價
登入
下載 Diffchecker 桌面版
比較文本
尋找兩個文字檔案之間的差異
工具
歷史
即時編輯器
摺疊未變更行
關閉換行
檢視
拆分
統一
比對精度
智能
單詞
字符
語法突出顯示
選擇語法
忽略
文字轉換
前往第一個差異
編輯輸入
Diffchecker Desktop
執行Diffchecker最安全的方式。取得Diffchecker桌面應用程式:您的差異永遠不會離開您的電腦!
取得桌面版
MCS 275 2022 worksheet 7 question 3
建立於
4 年前
差異永不過期
清除
匯出
分享
解釋
9 刪除
行
總計
刪除
字符
總計
刪除
要繼續使用此功能,請升級到
Diff
checker
Pro
查看價格
34 行
全部複製
25 新增
行
總計
新增
字符
總計
新增
要繼續使用此功能,請升級到
Diff
checker
Pro
查看價格
39 行
全部複製
複製
已複製
複製
已複製
def solvemaze
(M,path
=None
):
def solvemaze
_history
(M,path
_list
=None
, solved=False
):
"""
"""
複製
已複製
複製
已複製
Find a solution
to
maze `M`
Functions similarly
to
`solvemaze`, but returns a list of all paths
using a depth-first recursive
considered throughout solving process. Uses additional arg `solved`
search
.
to track whether solution has been found
.
"""
"""
複製
已複製
複製
已複製
print("Called with path={}".format(path))
if path
_list
==None:
if path
==None:
# no path specified, so start
# no path specified, so start
# at M.start
# at M.start
複製
已複製
複製
已複製
path
=
[M.start]
path
_list
=
[
[M.start]
] # Use a list of lists for path_list - one list for each path.
else:
print("Called with path={}".format(path_list[-1]))
path = path_list[-1]
if path[-1]==M.goal:
if path[-1]==M.goal:
# We've found a solution
# We've found a solution
print("SOLVED!")
print("SOLVED!")
複製
已複製
複製
已複製
return path
return path
_list, True # Set the "solved" variable to True
# next steps to consider
# next steps to consider
# (may include retracing our steps)
# (may include retracing our steps)
steps = M.free_neighbors(*path[-1])
steps = M.free_neighbors(*path[-1])
for s in steps:
for s in steps:
if len(path)>=2 and s == path[-2]:
if len(path)>=2 and s == path[-2]:
print("Not considering {}, as it would retrace our steps".format(s))
print("Not considering {}, as it would retrace our steps".format(s))
continue
continue
# Consider whether next step `s` leads to a solution
# Consider whether next step `s` leads to a solution
print("Considering next step {}".format(s))
print("Considering next step {}".format(s))
複製
已複製
複製
已複製
soln
= solvemaze
(M,path
+ [s]
)
next_path = path + [s]
if
soln != None
:
path_list.append(next_path)
soln
, solved
= solvemaze
_history
(M,path
_list
)
if
solved
:
# Some recursive call yielded a solution!
# Some recursive call yielded a solution!
print("Hooray, considering {} worked!".format(s))
print("Hooray, considering {} worked!".format(s))
複製
已複製
複製
已複製
return soln
return soln
, True # # Set the "solved" variable to True
# if we reach this line, step `s` only led to dead ends
# if we reach this line, step `s` only led to dead ends
# if we reach this line, then no continuation of `path`
# if we reach this line, then no continuation of `path`
# leads to a solution, only dead ends.
# leads to a solution, only dead ends.
複製
已複製
複製
已複製
# Still need to return path_list so we can visualize it later
print("Path {} leads only to dead ends".format(path))
print("Path {} leads only to dead ends".format(path))
複製
已複製
複製
已複製
return
None
return
path_list, False # Keep the "solved" variable as False.
已保存差異
原始文本
開啟檔案
def solvemaze(M,path=None): """ Find a solution to maze `M` using a depth-first recursive search. """ print("Called with path={}".format(path)) if path==None: # no path specified, so start # at M.start path = [M.start] if path[-1]==M.goal: # We've found a solution print("SOLVED!") return path # next steps to consider # (may include retracing our steps) steps = M.free_neighbors(*path[-1]) for s in steps: if len(path)>=2 and s == path[-2]: print("Not considering {}, as it would retrace our steps".format(s)) continue # Consider whether next step `s` leads to a solution print("Considering next step {}".format(s)) soln = solvemaze(M,path + [s]) if soln != None: # Some recursive call yielded a solution! print("Hooray, considering {} worked!".format(s)) return soln # if we reach this line, step `s` only led to dead ends # if we reach this line, then no continuation of `path` # leads to a solution, only dead ends. print("Path {} leads only to dead ends".format(path)) return None
更改後文本
開啟檔案
def solvemaze_history(M,path_list=None, solved=False): """ Functions similarly to `solvemaze`, but returns a list of all paths considered throughout solving process. Uses additional arg `solved` to track whether solution has been found. """ if path_list==None: # no path specified, so start # at M.start path_list = [[M.start]] # Use a list of lists for path_list - one list for each path. else: print("Called with path={}".format(path_list[-1])) path = path_list[-1] if path[-1]==M.goal: # We've found a solution print("SOLVED!") return path_list, True # Set the "solved" variable to True # next steps to consider # (may include retracing our steps) steps = M.free_neighbors(*path[-1]) for s in steps: if len(path)>=2 and s == path[-2]: print("Not considering {}, as it would retrace our steps".format(s)) continue # Consider whether next step `s` leads to a solution print("Considering next step {}".format(s)) next_path = path + [s] path_list.append(next_path) soln, solved = solvemaze_history(M,path_list) if solved: # Some recursive call yielded a solution! print("Hooray, considering {} worked!".format(s)) return soln, True # # Set the "solved" variable to True # if we reach this line, step `s` only led to dead ends # if we reach this line, then no continuation of `path` # leads to a solution, only dead ends. # Still need to return path_list so we can visualize it later print("Path {} leads only to dead ends".format(path)) return path_list, False # Keep the "solved" variable as False.
尋找差異