Diff
checker
文本
文本
图像
文档
Excel
文件夹
Legal
Enterprise
桌面版
定价
登录
下载 Diffchecker 桌面版
比较文本
查找两个文本文件之间的差异
工具
历史
实时编辑器
折叠未更改行
关闭换行
视图
拆分
统一
比对精度
智能
单词
字符
语法高亮
选择语法
忽略
文本转换
转到第一个差异
编辑输入
Diffchecker Desktop
运行Diffchecker最安全的方式。获取Diffchecker桌面应用:您的差异永远不会离开您的电脑!
获取桌面版
MCS 275 Spring 2023 Homework 6
创建于
3年前
差异永不过期
清除
导出
分享
解释
14 删除
行
总计
删除
字符
总计
删除
要继续使用此功能,请升级到
Diff
checker
Pro
查看价格
36 行
全部复制
22 添加
行
总计
添加
字符
总计
添加
要继续使用此功能,请升级到
Diff
checker
Pro
查看价格
42 行
全部复制
复制
已复制
复制
已复制
def
depth_first_maze_solution
(M,
path=None,verbose=False):
def
can_be_solved_maxlen
(M,
k,
path=None,verbose=False):
"""
"""
复制
已复制
复制
已复制
Find
solution to
Maze
`M`
that begins with
`path`
(
if given)
,
Returns True if a
solution to
`M`
can be found which is of length
returning either that solution as a list of Point2 objects or
at most `k` (starting from
`path`
,
if given)
. Else, returns False.
None if no such solution exists.
"""
"""
复制
已复制
复制
已复制
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
True
# If there are no more steps to take and we haven't reached the goal,
# then don't make another recursive call
if k == 0:
return False
possible_next_locations = M.free_neighbors(path[-1])
possible_next_locations = M.free_neighbors(path[-1])
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)
# Reduce `k` by 1 because we've now made another move.
if solution:
# None is falsy, while a nonempty list is truthy
solution =
can_be_solved_maxlen
(M,
k-1,
new_path,verbose)
return
solution
if solution:
# If we've found `True` at any point in our search, return True.
return
True
# What now? If we end up here, it means no next step leads to a solution
# What now? If we end up here, it means no next step leads to a solution
# Hence `path` leads to only dead ends
# Hence `path` leads to only dead ends
# We therefore BACKTRACK
# We therefore BACKTRACK
if verbose:
if verbose:
print("GIVING UP ON:",path)
print("GIVING UP ON:",path)
复制
已复制
复制
已复制
return
None
return
False
已保存差异
原始文本
打开文件
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 can_be_solved_maxlen(M, k, path=None,verbose=False): """ Returns True if a solution to `M` can be found which is of length at most `k` (starting from `path`, if given). Else, returns False. """ 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 True # If there are no more steps to take and we haven't reached the goal, # then don't make another recursive call if k == 0: return False 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 # Reduce `k` by 1 because we've now made another move. solution = can_be_solved_maxlen(M, k-1, new_path,verbose) if solution: # If we've found `True` at any point in our search, return True. return True # 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 False
查找差异