diff --git a/search.py b/search.py index 7fb4489..86ad199 100644 --- a/search.py +++ b/search.py @@ -100,6 +100,8 @@ def depthFirstSearch(problem: SearchProblem): path = path + [direction] visited.append(new_state) isGoal = problem.isGoalState(new_state) + if isGoal: + break for (_state, _direction, _cost) in problem.getSuccessors(new_state): # (state, direction, cost) stack.push(((_state, _direction, _cost), path)) path = path[1:] @@ -130,6 +132,8 @@ def breadthFirstSearch(problem: SearchProblem): path = path + [direction] visited.append(new_state) isGoal = problem.isGoalState(new_state) + if isGoal: + break for (_state, _direction, _cost) in problem.getSuccessors(new_state): # (state, direction, cost) queue.push(((_state, _direction, _cost), path)) path = path[1:] @@ -161,6 +165,8 @@ def uniformCostSearch(problem: SearchProblem): costs = costs + cost visited.append(new_state) isGoal = problem.isGoalState(new_state) + if isGoal: + break for (_state, _direction, _cost) in problem.getSuccessors(new_state): # (state, direction, cost) queue.push(((_state, _direction, _cost), path, costs), costs+_cost) path = path[1:]