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