对象的广度优先遍历

发布于 2024-10-26 16:30:00 字数 744 浏览 3 评论 0原文

我正在编写一个解决益智游戏的程序,它会找到棋盘上所有可能的动作,并将所有可能的结果棋盘放入一个对象中。然后它会找到结果棋盘的所有可能的走法,依此类推。该对象看起来像这样:

{
  "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 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海之角 2024-11-02 16:30:00

递归。

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

编辑:对于广度优先搜索,请尝试这样的操作。它不使用递归,而是迭代不断增长的队列。

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

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();
    }
}
七度光 2024-11-02 16:30:00

未完全测试:

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);

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);
月下凄凉 2024-11-02 16:30:00

我没有 JavaScript 描述,但我通常会通过保留未探索节点的队列来实现。

  1. 仅从队列中的根节点开始。
  2. 从队列前面弹出一个项目
  3. 探索它将探索过程中找到的所有节点添加到队列后面
  4. 检查队列中是否有任何节点 如果有则返回到步骤 2
  5. 您的完成

另外还有一些伪pod维基百科页面以及更多解释
此处

Google 快速搜索还发现了类似的算法,可以满足您的目的
此处

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文