Diff
checker
テキスト
テキスト
画像
ドキュメント
Excel
フォルダ
Legal
Enterprise
デスクトップ
料金
ログイン
Diffchecker デスクトップのダウンロード
テキスト比較
2 つのテキスト ファイルの違いを見つける
ツール
履歴
ライブエディター
未変更行を折りたたむ
折り返しなし
レイアウト
分割
統合
比較精度
スマート
単語
文字
シンタックスハイライト
構文を選択
無視
テキスト変換
最初の差分へ移動
入力を編集
Diffchecker Desktop
Diffcheckerを実行する最も安全な方法。Diffchecker Desktopアプリを入手:あなたの差分はコンピューターから出ることはありません!
Desktopを入手
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
違いを見つける