python反恐精英代码类似的编程_敲代码学Python:CS188之实现深度优先搜索

这篇博客介绍了如何使用Python实现深度优先搜索算法,通过伪代码和具体代码示例展示了在图形搜索问题中的应用。作者提供了深度受限树搜索的伪代码,并解释了代码逻辑,同时给出了实际运行的视频链接。
摘要由CSDN通过智能技术生成

先上运行代码后的视频:c14d656d303fc6af7c5c57e461366183.png人工智能导论之深度优先搜索演示https://www.zhihu.com/video/11736416734263787520ef325099065ab0f554a694eae504b34.png深度优先搜索演示之大地图https://www.zhihu.com/video/1173667844352057344

再上一个深度受限树搜索算法的伪代码表示:

最后贴上我的代码

def depthFirstSearch(problem):

"""

Search the deepest nodes in the search tree first.

Your search algorithm needs to return a list of actions that reaches the

goal. Make sure to implement a graph search algorithm.

To get started, you might want to try some of these simple commands to

understand the search problem that is being passed in:

print("Start:", problem.getStartState())

print("Is the start a goal?", problem.isGoalState(problem.getStartState()))

print("Start's successors:", problem.getSuccessors(problem.getStartState()))

"""

"*** YOUR CODE HERE ***"

def Recursive_DFS(node,problem,solution,closedSet):

# 测试当前送进来的节点是否满足目标要求,如果是一个可行解,就返回行动方案

if problem.isGoalState(node):

return solution

# 否则,就构造循环遍历这个节点的所有子节点

else:

for child,direction,cost in problem.getSuccessors(node):

# 如果子节点是还没有计算过的节点,就开始继续往下走

if child not in closedSet:

# 在行动方案中增加当前子节点的执行动作

solution.append(direction)

# 然后把这个子节点增加到计算过的节点集合中

closedSet.add(child)

# 调用递归函数继续从当前子节点往下计算

result = Recursive_DFS(child,problem,solution,closedSet)

# 判定一下计算结果

if result!=False:

# 如果找到了可行路线,则把行动方案返回出去吧

# 附带一句:return有停止当前函数继续运行的效果哟!

return solution

else:

# 如果从这个节点往下没有可行路线,就要撤销刚才的执行动作

solution.pop()

# 这个else和for对应,表示所有的子节点都遍历过了,没找到合适的行动方案,查找失败

else:

return False

solution = []

# 因为我们的数据结构是图,不是树,所以要构造一个集合用于判定某个节点是否已经算过了,否则会死循环

closedSet = set()

# 一开始把初始位置加入上述集合中,意味着Pacman不能经过这个节点了

closedSet.add(problem.getStartState())

# 调用递归函数进行问题求解,多一个参数就是上面的集合

solution = Recursive_DFS(problem.getStartState(),problem,solution,closedSet)

return solution

因为CSS188的作业要求尽量按照课堂幻灯的算法描述进行编程,所以下方的代码和上面的算法描述有一些不一致,但是我的注释可以帮助你理解这个程序,两个算法的基本思想还是类似的:

def depthFirstSearch(problem):

"""Search the deepest nodes in the search tree first.Your search algorithm needs to return a list of actions that reaches thegoal. Make sure to implement a graph search algorithm.To get started, you might want to try some of these simple commands tounderstand the search problem that is being passed in:print("Start:", problem.getStartState())print("Is the start a goal?", problem.isGoalState(problem.getStartState()))print("Start's successors:", problem.getSuccessors(problem.getStartState()))"""

"*** YOUR CODE HERE ***"

# 初始化搜索状态

fringe = util.Stack()

node = {"state":problem.getStartState(), "path":[], "cost":0}

fringe.push(node)

explored = set()

# 构造循环展开搜索树

while (not fringe.isEmpty()):

# 获得待判定的叶子节点

node = fringe.pop()

# 判断节点是否满足目标要求,如果是一个可行解,就返回行动方案

if problem.isGoalState(node["state"]):

return node["path"]

# 否则,就继续从这个叶子节点往下展开

else:

# 先判断一下这个节点是不是已经展开过了,避免重复展开

if node["state"] not in explored:

for nextnode in problem.getSuccessors(node["state"]):

# 为了适应可能的数据结构为图,必须判定叶子节点是否已经访问过

if nextnode[0] not in explored:

nextnode = {"state":nextnode[0],

"path":node["path"]+[nextnode[1]],

"cost":node["cost"]+nextnode[2]}

# 如果没有访问过,就将叶子节点添加到待搜索的节点集合中

fringe.push(nextnode)

# 最后不要忘记把搜索过的节点添加到访问过的节点集合中

explored.add(node["state"])

GitHub地址:xuejing80/learnpython​github.comeaec947d5fc713c86eeac971d6b2ae6b.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值