Diff
checker
文本
文本
圖像
文檔
Excel
文件夾
Legal
Enterprise
桌面版
定價
登入
下載 Diffchecker 桌面版
比較文本
尋找兩個文字檔案之間的差異
工具
歷史
即時編輯器
摺疊未變更行
關閉換行
檢視
拆分
統一
比對精度
智能
單詞
字符
語法突出顯示
選擇語法
忽略
文字轉換
前往第一個差異
編輯輸入
Diffchecker Desktop
執行Diffchecker最安全的方式。取得Diffchecker桌面應用程式:您的差異永遠不會離開您的電腦!
取得桌面版
MCS 275 Spring 2023 Homework 6 (method 2)
建立於
3 年前
差異永不過期
清除
匯出
分享
解釋
13 刪除
行
總計
刪除
字符
總計
刪除
要繼續使用此功能,請升級到
Diff
checker
Pro
查看價格
36 行
全部複製
20 新增
行
總計
新增
字符
總計
新增
要繼續使用此功能,請升級到
Diff
checker
Pro
查看價格
33 行
全部複製
複製
已複製
複製
已複製
def depth_first_
maze_solution
(M,path=None,verbose=False):
def depth_first_
all_
maze_solution
s
(M,path=None,verbose=False):
"""
"""
複製
已複製
複製
已複製
Find
solution
to Maze `M` that begin
s
with `path` (if given),
Find
all
solution
s
to Maze `M` that begin
with `path` (if given),
returning
either that solution as
a
list
of Point2 objects or
returning
a list where every entry is itself
a
sub
list
representing a
None if no such
solution
exists
.
single
solution
to the maze
.
"""
"""
if path == None:
if path == None:
# no path was specified, initialize it with [M.start]
# no path was specified, initialize it with [M.start]
path = [ M.start ]
path = [ M.start ]
if verbose:
if verbose:
print("Considering:",path)
print("Considering:",path)
if path[-1] == M.goal:
if path[-1] == M.goal:
# path ends with goal, meaning it's a solution
# path ends with goal, meaning it's a solution
複製
已複製
複製
已複製
return
path
return
[path] # Put the
path
into a list
possible_next_locations = M.free_neighbors(path[-1])
possible_next_locations = M.free_neighbors(path[-1])
複製
已複製
複製
已複製
solutions = []
for x in possible_next_locations:
for x in possible_next_locations:
if x in path:
if x in path:
# skip x
# skip x
continue # do not execute the rest of the loop body
continue # do not execute the rest of the loop body
# immediately begin the next iteration.
# immediately begin the next iteration.
# x should be considered
# x should be considered
new_path = path + [x]
new_path = path + [x]
# Ask for a solution that continues from new_path
# Ask for a solution that continues from new_path
複製
已複製
複製
已複製
solution = depth_first_
maze_solution
(M,new_path,verbose)
solution = depth_first_
all_
maze_solution
s
(M,new_path,verbose)
if
solution
: # None is falsy, while a nonempty list is truthy
if
len(
solution
) > 0:
return
solution
solutions.extend(solution) # Keep all
solution
s found from recursive call
複製
已複製
複製
已複製
# What now? If we end up here, it means no next step leads to a solution
# Always return our list of solutions (which may be empty)
# Hence `path` leads to only dead ends
return
solutions
# We therefore BACKTRACK
if verbose:
print("GIVING UP ON:",path)
return
None
已保存差異
原始文本
開啟檔案
def depth_first_maze_solution(M,path=None,verbose=False): """ Find solution to Maze `M` that begins with `path` (if given), returning either that solution as a list of Point2 objects or None if no such solution exists. """ if path == None: # no path was specified, initialize it with [M.start] path = [ M.start ] if verbose: print("Considering:",path) if path[-1] == M.goal: # path ends with goal, meaning it's a solution return path possible_next_locations = M.free_neighbors(path[-1]) for x in possible_next_locations: if x in path: # skip x continue # do not execute the rest of the loop body # immediately begin the next iteration. # x should be considered new_path = path + [x] # Ask for a solution that continues from new_path solution = depth_first_maze_solution(M,new_path,verbose) if solution: # None is falsy, while a nonempty list is truthy return solution # What now? If we end up here, it means no next step leads to a solution # Hence `path` leads to only dead ends # We therefore BACKTRACK if verbose: print("GIVING UP ON:",path) return None
更改後文本
開啟檔案
def depth_first_all_maze_solutions(M,path=None,verbose=False): """ Find all solutions to Maze `M` that begin with `path` (if given), returning a list where every entry is itself a sublist representing a single solution to the maze. """ if path == None: # no path was specified, initialize it with [M.start] path = [ M.start ] if verbose: print("Considering:",path) if path[-1] == M.goal: # path ends with goal, meaning it's a solution return [path] # Put the path into a list possible_next_locations = M.free_neighbors(path[-1]) solutions = [] for x in possible_next_locations: if x in path: # skip x continue # do not execute the rest of the loop body # immediately begin the next iteration. # x should be considered new_path = path + [x] # Ask for a solution that continues from new_path solution = depth_first_all_maze_solutions(M,new_path,verbose) if len(solution) > 0: solutions.extend(solution) # Keep all solutions found from recursive call # Always return our list of solutions (which may be empty) return solutions
尋找差異