dfs attempt
This commit is contained in:
@@ -87,7 +87,48 @@ def depthFirstSearch(problem):
|
|||||||
print("Start's successors:", problem.getSuccessors(problem.getStartState()))
|
print("Start's successors:", problem.getSuccessors(problem.getStartState()))
|
||||||
"""
|
"""
|
||||||
"*** YOUR CODE HERE ***"
|
"*** YOUR CODE HERE ***"
|
||||||
util.raiseNotDefined()
|
from game import Directions
|
||||||
|
from util import Stack
|
||||||
|
|
||||||
|
actions, visited, fringe = [], [], Stack()
|
||||||
|
paths = {}
|
||||||
|
goal = None
|
||||||
|
|
||||||
|
print("Start:", problem.getStartState())
|
||||||
|
print("Is the start a goal?", problem.isGoalState(problem.getStartState()))
|
||||||
|
print("Start's successors:", problem.getSuccessors(problem.getStartState()))
|
||||||
|
|
||||||
|
fringe.push((problem.getStartState(), '', 0))
|
||||||
|
while fringe.isEmpty() is False:
|
||||||
|
curr_state = fringe.pop()
|
||||||
|
|
||||||
|
if curr_state in visited:
|
||||||
|
continue
|
||||||
|
|
||||||
|
visited.append(curr_state)
|
||||||
|
|
||||||
|
if problem.isGoalState(curr_state):
|
||||||
|
goal = curr_state
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
|
# if curr_state[1]:
|
||||||
|
# actions.append(curr_state[1])
|
||||||
|
|
||||||
|
for successor in problem.getSuccessors(curr_state[0]):
|
||||||
|
fringe.push(successor)
|
||||||
|
paths[successor[0]] = curr_state
|
||||||
|
|
||||||
|
curr_pos = goal
|
||||||
|
print(goal)
|
||||||
|
# while curr_pos[0] != problem.getStartState():
|
||||||
|
# actions.append(curr_pos[1])
|
||||||
|
# curr_pos = paths[curr_pos[0]]
|
||||||
|
# print(curr_pos)
|
||||||
|
# # curr_pos = (problem.getStartState(), '', 0)\
|
||||||
|
# print(actions)
|
||||||
|
|
||||||
|
return [Directions.WEST];
|
||||||
|
|
||||||
def breadthFirstSearch(problem):
|
def breadthFirstSearch(problem):
|
||||||
"""Search the shallowest nodes in the search tree first."""
|
"""Search the shallowest nodes in the search tree first."""
|
||||||
|
|||||||
Reference in New Issue
Block a user