先上视频:吃完所有角落的豆豆视频演示https://www.zhihu.com/video/1177971208993325056
简单说一下我的思路:
搜索状态的表达是搜索算法中的重要部分,在本题中,为了记录下吃豆人的吃豆过程,我把吃豆人所经过的包含豆豆的坐标放在搜索状态中,这样就可以从搜索状态中判定它是否完成了当前地图的任务目标。
贴代码吧:
class CornersProblem(search.SearchProblem):
"""This search problem finds paths through all four corners of a layout.You must select a suitable state space and successor function"""
def __init__(self, startingGameState):
"""Stores the walls, pacman's starting position and corners."""
self.walls = startingGameState.getWalls()
self.startingPosition = startingGameState.getPacmanPosition()
top, right = self.walls.height-2, self.walls.width-2
self.corners = ((1,1), (1,top), (right, 1), (right, top))
for corner in self.corners:
if not startingGameState.hasFood(*corner):
print('Warning: no food in corner ' + str(corner))
self._expanded = 0 # DO NOT CHANGE; Number of search nodes expanded
# Please add any code here which you would like to use
# in initializing the problem
"*** YOUR CODE HERE ***"
# 这里我没有用上,^_^
def getStartState(self):
"""Returns the start state (in your state space, not the full Pacman statespace)"""
"*** YOUR CODE HERE ***"
# 依然以吃豆人的初始位置作为起始状态
return self.startingPosition
def isGoalState(self, state):