Breadth first traversal of object

ε祈祈猫儿з 提交于 2019-12-04 08:01:46

问题


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 possible moves for the resulting boards, and so on. The object will look something like this:

{
  "board": {
      "starts": [[0,0],[0,3]],
      "blocks": [[3,0],[3,3]],
      "ends":   [[2,4]]
  },
  "possibleMoves": [
    {
      "board": {
        "starts": [[0,0],[2,3]],
        "blocks": [[3,0],[3,3]],
        "ends":   [[2,4]]
      },
      "possibleMoves":[
        {
          "board": {},
          "possibleMoves": [{}]
        }
      ]
    },
    {
      "board": {
        "starts": [[0,3]],
        "blocks": [[3,0],[3,3]],
        "ends":   [[2,4]]
      },
      "possibleMoves":[{}]
    }]
}

I can figure out how to add the possible moves from the top-level board, but I cannot figure out how to loop through all the resulting boards in the second level and figure out their possible moves, and then loop through all the third level boards and so on. How can I add the possible moves and traverse the object using a breadth-first search?


回答1:


Recursion.

function traverse(state) {
    handle(state.board);
    if (state.possibleMoves) {
        $.each(state.possibleMoves, function(i, possibleMove) {
             traverse(possibleMove);
        });
    }
}

EDIT: For a breadth-first search, try something like this. It doesn't use recursion, but instead iterates over a growing queue.

function traverse(state) {
    var queue = [],
        next = state;
    while (next) {
        if (next.possibleMoves) {
            $.each(next.possibleMoves, function(i, possibleMove) {
                queue.push(possibleMove);
            });
        }
        next = queue.shift();
    }
}



回答2:


Not completely tested:

var oo = {
    board: {
        starts: [[0,0],[0,3]],
        blocks: [[3,0],[3,3]],
        ends:   [[2,4]]
    },
    possibleMoves: [{
        board: {
            starts: [[0,0],[2,3]],
            blocks: [[3,0],[3,3]],
            ends:   [[2,4]]
        },
    }],
};


function traverseObject (o) {
    for (var prop in o) {
        if (typeof o[prop] == "array" || typeof o[prop] == "object") {
            traverseObject(o[prop]);
            console.log(prop);
        } else {
            console.log(prop, "=", o[prop]);
        }
    }
}

traverseObject(oo);



回答3:


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



来源:https://stackoverflow.com/questions/5411270/breadth-first-traversal-of-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!