From fc86bf573bb36c136b1f3b1d1302777bbb74477b Mon Sep 17 00:00:00 2001 From: Arjun Patel Date: Thu, 7 Feb 2019 13:16:32 -0800 Subject: [PATCH] dfs attempt --- search.py | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/search.py b/search.py index cd330bf..398adf2 100644 --- a/search.py +++ b/search.py @@ -87,7 +87,48 @@ def depthFirstSearch(problem): print("Start's successors:", problem.getSuccessors(problem.getStartState())) """ "*** 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): """Search the shallowest nodes in the search tree first."""