From f0c5c6d84a49b71ac2ac623b2e03703717119d27 Mon Sep 17 00:00:00 2001 From: snsd0805 Date: Tue, 26 Mar 2024 22:10:59 +0800 Subject: [PATCH] fix: return when isGoal --- search.py | 6 ++++++ 1 file changed, 6 insertions(+) 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:]