DFS ,BFS Pseudocode

 

// Simple dfs
function dfs(node position)
    color(position)
    for each successor adjacent to node "position"
        if successor is colored, skip it
        if next is the goal node, stop the search
        else, dfs(successor)
    end
end

 

// Simple bfs
structure node
    position pos
    node *parent
end

function bfs(node start_position)
    add start_position to the queue
    while the queue is not empty
        pop a node off the queue, call it "item"
        color item on the graph // make sure we don't search it again
        generate the 8 successors to item
        set the parent of each successor to "item" // this is so we can backtrack our final solution
        for each successor
            if the successor is the goal node, end the search
            else, push it to the back of the queue // So we can search this node
        end
    end
    
    if we have a goal node, look at its ancestry to find the path (node->parent->parent->parent..., etc)
    if not, the queue was empty and we didn't find a path :^\
end

 

转载于:https://www.cnblogs.com/yk00/p/3214366.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值