Diff
checker
Texte
Texte
Images
Documents
Excel
Dossiers
Legal
Enterprise
Application de bureau
Prix
Se connecter
Télécharger Diffchecker Desktop
Comparer le texte
Trouver la différence entre deux fichiers texte
Outils
Historique
Éditeur live
Cacher identiques
Sans retour à la ligne
Vue
Divisé
Unifié
Niveau de précision
Intelligent
Mot
Caractère
Coloration syntaxique
Choisir la syntaxe
Ignorer
Transformer le texte
Aller au premier écart
Modifier l'entrée
Diffchecker Desktop
La façon la plus sécurisée d'utiliser Diffchecker. Obtenez l'application Diffchecker Desktop : vos diffs ne quittent jamais votre ordinateur !
Obtenir Desktop
MCS 275 Spring 2023 Homework 6 (method 2)
Créé
il y a 3 ans
Le diff n'expire jamais
Effacer
Exporter
Partager
Expliquer
13 suppressions
Lignes
Total
Supprimé
Caractères
Total
Supprimé
Pour continuer à utiliser cette fonctionnalité, passez à
Diff
checker
Pro
Voir les prix
36 lignes
Copier tout
20 ajouts
Lignes
Total
Ajouté
Caractères
Total
Ajouté
Pour continuer à utiliser cette fonctionnalité, passez à
Diff
checker
Pro
Voir les prix
33 lignes
Copier tout
Copier
Copié
Copier
Copié
def depth_first_
maze_solution
(M,path=None,verbose=False):
def depth_first_
all_
maze_solution
s
(M,path=None,verbose=False):
"""
"""
Copier
Copié
Copier
Copié
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
Copier
Copié
Copier
Copié
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])
Copier
Copié
Copier
Copié
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
Copier
Copié
Copier
Copié
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
Copier
Copié
Copier
Copié
# 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
Différences enregistrées
Texte d'origine
Ouvrir un fichier
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
Texte modifié
Ouvrir un fichier
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
Trouver la différence