premiere question resolue
This commit is contained in:
parent
3700417023
commit
a51b467a4f
1 changed files with 29 additions and 1 deletions
|
@ -87,8 +87,36 @@ def depthFirstSearch(problem):
|
|||
print "Start's successors:", problem.getSuccessors(problem.getStartState())
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
maPile = util.Stack()
|
||||
maListeFermee = []
|
||||
# Verifier si le depart est un but
|
||||
if problem.isGoalState(problem.getStartState()):
|
||||
return []
|
||||
# Mettre le premier noeud et un chemin vide dans la pile
|
||||
maPile.push((problem.getStartState(),list()))
|
||||
# Rechercher tant que la pile n'est pas vide
|
||||
while (not maPile.isEmpty()):
|
||||
# Extraire le noeud courant
|
||||
noeudCourant, listeDirections = maPile.pop()
|
||||
# print "Noeud courant: ", noeudCourant
|
||||
# La marquer comme visite
|
||||
maListeFermee.append(noeudCourant)
|
||||
# Verifier si c'est un but
|
||||
if(problem.isGoalState(noeudCourant)):
|
||||
# print "Nous avons atteint le but !"
|
||||
return listeDirections
|
||||
break
|
||||
# Effectuer la recherche
|
||||
for s in problem.getSuccessors(noeudCourant):
|
||||
# Decomposer le tuple pour faciliter la lecture
|
||||
succNoeudCourant, succDirection, succCost = s
|
||||
# Verifier si le noeud n'est pas deja visite
|
||||
if (succNoeudCourant not in maListeFermee):
|
||||
# Dans ce cas, ajouter le noeud et le chemin pour s'y rendre a la pile
|
||||
# print "Ajout du noeud à la pile: ", succNoeudCourant
|
||||
maPile.push((succNoeudCourant,listeDirections+[succDirection]))
|
||||
util.raiseNotDefined()
|
||||
|
||||
|
||||
def breadthFirstSearch(problem):
|
||||
"""Search the shallowest nodes in the search tree first."""
|
||||
"*** YOUR CODE HERE ***"
|
||||
|
|
Loading…
Reference in a new issue