fix: return when isGoal

This commit is contained in:
snsd0805 2024-03-26 22:10:59 +08:00
parent 7a31882480
commit f0c5c6d84a
Signed by: snsd0805
GPG Key ID: 569349933C77A854

View File

@ -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:]