diff --git a/search.py b/search.py index e66a5ff..eed2cd6 100644 --- a/search.py +++ b/search.py @@ -118,7 +118,36 @@ def depthFirstSearch(problem: SearchProblem): def breadthFirstSearch(problem: SearchProblem): """Search the shallowest nodes in the search tree first.""" "*** YOUR CODE HERE ***" - util.raiseNotDefined() + actions = [] + + curr_state = problem.getStartState() + isGoal = problem.isGoalState(curr_state) + + visited = set() + queue = util.Queue() + + while not isGoal: + # push in the Stack + successors = problem.getSuccessors(curr_state) + for (state, direction, cost) in successors: + if state not in visited: + visited.add(state) + queue.push({ + 'state': (state, direction, cost), + 'actions': actions + }) + + # check whether reachable + if queue.isEmpty(): + return [] + else: + obj = queue.pop() + (curr_state, direction, cost) = obj['state'] + actions = obj['actions'] + [ direction ] + isGoal = problem.isGoalState(curr_state) + + return actions + def uniformCostSearch(problem: SearchProblem): """Search the node of least total cost first."""