searchAgents.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:cs188_tbf 作者: loren-jiang 项目源码 文件源码
def getSuccessors(self, state):
        """
        Returns successor states, the actions they require, and a cost of 1.

         As noted in search.py:
            For a given state, this should return a list of triples, (successor,
            action, stepCost), where 'successor' is a successor to the current
            state, 'action' is the action required to get there, and 'stepCost'
            is the incremental cost of expanding to that successor
        """

        successors = []
        for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]:
            # Add a successor state to the successor list if the action is legal
            # Here's a code snippet for figuring out whether a new position hits a wall:
            #   x,y = currentPosition
            #   dx, dy = Actions.directionToVector(action)
            #   nextx, nexty = int(x + dx), int(y + dy)
            #   hitsWall = self.walls[nextx][nexty]

            "*** YOUR CODE HERE ***"
            cornersNotVisited = state[1]
            x,y = state[0]
            dx, dy = Actions.directionToVector(action)
            nextx, nexty = int(x + dx), int(y + dy)
            hitsWall = self.walls[nextx][nexty]
            if not hitsWall:
                if (nextx, nexty) in state[1]:
                    cornersNotVisited = filter(lambda a: a != (nextx, nexty), cornersNotVisited) #remove visited corner
                nextState = ((nextx, nexty), cornersNotVisited)
                cost = 1
                successors.append((nextState, action, cost))

        self._expanded += 1 # DO NOT CHANGE
        return successors
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号