Breadth first traversal of object

前端 未结 3 1433
南旧
南旧 2021-02-02 04:47

I am making a program that solves a puzzle game, and it finds all the possible moves on a board and puts all the possible resulting boards in an object. Then it finds all the po

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 04:52

    I don't have a JavaScript description but i would generally do it by keeping a queue of unexplored nodes.

    1. Start with only the root node in the queue.
    2. Pop an item from the front of the queue
    3. Explore it add all of the nodes found during exploration to the back of the queue
    4. Check if there are any nodes in the queue if there are go back to step 2
    5. Your done

    Also there is some pseudopod on the Wikipedia page as well as some more explanations HERE

    Also a quick Google search turned up a similar algorithm that could be bent to your purpose HERE

提交回复
热议问题