【问题标题】:Finding a path using multi-dimensional arrays in jsjs中使用多维数组查找路径
【发布时间】:2014-01-27 17:22:56
【问题描述】:

我有一个存储在多维数组中的 x-y 网格。 x-y 网格中的每个点都有一个值。

例子:

var xy = [
    [0,3,1,1,0],
    [0,0,2,2,1],
    [0,0,1,1,0]
];

假设 var xy 的布局类似于 x-y 网格(例如 x 1 和 y 2 将是 3。

这是一个更大的此类变量的“打印输出”,具有更大的高度和宽度:

   (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) (11) (12) (13)
(1) 0   0   0   1   1   1   2   2   1    1    0    0    0
(2) 0   0   1   1   1   2   2   3   2    2    1    0    0
(3) 0   0   0   1   2   2   3   3   2    1    0    0    0
(4) 0   4   0   1   1   1   2   2   1    0    0    0    8
(5) 0   0   0   0   0   0   1   1   0    0    0    0    4
(6) 0   0   0   0   9   9   9   0   0    0    0    0    0 
(7) 0   0   0   0   9   9   0   0   0    0    0    0    0

现在,为了举例,假设上面的布局是一张地图,就像一个棋盘。对于初学者,不注意网格上每个点的值,假设我们想知道“游戏块”可以从哪些方格到达,比如 x4 y8 ....我们可以在 js 中做这样的事情:

 var n = ,//total # of items in grid
    a = ,//a certain point on the grid
    r = 5; //range of movement
 for(var i = 0; i < n; i++){ //this would be a double for or something similar to go through the grid properly, but let's pretend i is a given point on the grid.
    Math.abs(((a.xLocation + a.yLocation) - (i.xLocation + i.yLocation)) <= r) //if the positive difference between the current point in the loop and the location of the certain point is less than or equal to the range....true!
 }

所以在上面的粗略示例中,A 点的物体可以移动 5 步,对角线、垂直、水平等等。我们不知道方向,只知道运动的潜力。

这很容易弄清楚。我仍然想弄清楚的部分是:根据网格的 VALUES,您如何知道点 a 处的事物是否可以到达点 i。 0 表示没有增强,但 1 或 2 需要 1 或 2 或任何额外的运动才能到达.....你如何计算地图上的每个点?同样,不知道哪个方向或路径——它是否是最优的。

澄清:想象一个棋盘。每个方形棋盘都有一个值,表示到达那里需要多少额外的移动点。一个给定的棋子,完全不使用国际象棋规则,可以在任何方向移动,比如说,5个“移动点”。许多方格的值为 0,因此不需要进一步消耗移动点。但是一个 1 的方格总共需要 2 个移动点,2 总共需要 3 个移动点,等等。实际上,您可以很容易地将 1 加到下面的所有方格中,以确定相邻方格中的棋子是否可以移动到那里。随你喜欢。我只是在寻找某种可以得出答案的论坛或建议。在这里,看看这个简单得多的例子。

    (1) (2) (3)
(1)  0   1   3
(2)  1   X   2
(3)  2   0   1

把它想象成一个游戏,每个方块都代表某种地形劣势。有些路径比其他路径更容易,但其他路径更直接。您可以采取任何路径到达某个方格,但在移动之前,哪些方格是合法的,哪些不是?所以我们的作品在 X2 Y2 上,对吧?他有5个移动点。他想知道他可以搬到哪些地方。好吧,他可以移动到其中任何一个。但是 X1Y1 将花费 1 个移动点,X1Y2 将花费 2,X1Y3 将花费 4,等等等等。很容易计算出来。但是如果棋盘更大,并且每个潜在的(未知的)移动都需要积分,那么他可以移动到哪些方格而他不能移动到哪些方格?这有意义吗?

编辑 2:一个稍微复杂一点的例子:

    (1) (2) (3) (4) (5)
(1)  0   1   3   0   0
(2)  1   X   2   1   0
(3)  2   0   1   0   0
(4)  1   0   0   1   3
(5)  0   0   0   0   4

所以我们在这个例子中的部分再次出现在 X2Y2 中,但他想知道,对于每个方格,他是否可以到达那里 - 布尔值,是或否。只有九个方格很容易,但随着网格的增长,复杂性也会增加。我当然可以手动完成——他能到达 X4Y4 吗?是的。但是以编程方式,我如何得到这个?

EDIT3:网格的大小没有意义,我刚刚意识到。这实际上是范围的大小。例如,如果移动范围是 5,我只需要计算出每个方向五个方格的方格的可行性。这样就简化了一点。

EDIT4:我想我有个更好的主意。看最外圈5出。是否大于 0?那就不要。下一个最外圈(4 出)。大于 1?不,下一个最外圈。大于 2?那就不要。等等。这会起作用还是会导致不正确的结果?

首选 js 或 jQuery 的答案(甚至只是朝着正确的方向),但即使您可以通过逻辑工作,我也可以将其翻译成 js 或 jquery。

【问题讨论】:

  • 完全不清楚你的增强是如何工作的。通过阅读本文,我认为这意味着当运动范围 r=5 时,网格的值为 v,然后实际运动 am = r*v,所以如果 v=0 am=0,如果 v=2 am=10 等等。这是附近有什么地方吗?
  • 并非如此。我将尝试在上面的文字中澄清我实际上想要做的事情。一瞬间!谢谢
  • 我看到了是什么绊倒了你。我说了 1 或 2 次 - 我的意思是额外的 1 或 2 次(其中 (x1) 是次,但仍然......模棱两可。稍微改变了语言,添加了另外两个示例。
  • 是这个主意吗?我在一个有 5 个能量单位的牢房里。我可以移动到任何相邻的单元格,如果我移动到的单元格的值为 2,那么我的 2 个能量单位被吸收,我有 3 个能量单位。只要我有 1 个或多个能量单位,并且我想要移动的单元格的值小于或等于我剩余的能量单位,我就可以移动。当我的能量达到 0 时,我必须等待一些事件,而我的能量会重新充电,或者我只是停下来。问题是对于任何给定的细胞,我可以用一定的能量到达哪些其他细胞?

标签: javascript math multidimensional-array logic coordinates


【解决方案1】:

我认为你想做的是一种基本的搜索,在每次迭代中你检查周围的方块。我想出了一个样机示例with this jsfiddle。打开控制台,点击运行,它应该会打印出示例地图以及从 (2, 2) 的 3 步可以到达的地方。

里面有一些额外的设置代码,但主要代码是search_rec函数:

function search_rec(
      map, // The input 'difficulty' map
      output, // The output map (puts '1's in spots that can be reached)
              // Should be the same size as the input map
      x, y, // The current/starting position
      limit) { // The number of spaces you are allowed to move

    // If the number of spaces allowed to move is negative, then we can't
    // reach this position, so we stop
    if (limit < 0) {
        return;
    }

    // Otherwise, if the limit is >= 0, then we can make it here and we
    // store that in the output
    output[y][x] = 1;

    // Now, for each of the four directions
    // Check if we're at a map boundary
    if (x > 0) {
        // If we're not, then we recurse, moving our starting point in
        // the given direction, and lowering our limit by 1 (for the
        // base movement) as well as the 'difficulty' of the terrain of
        // the space we're moving into

        search_rec(map, output, x - 1, y,
        //                      ^ change position
                      limit           - 1          - map[y][x - 1]);
        //   lower limit ^ by the base ^ and the difficulty ^
    }
    if (x < map[0].length - 1) {
        search_rec(map, output, x + 1, y, limit - map[y][x + 1] - 1);
    }
    if (y > 0) {
        search_rec(map, output, x, y - 1, limit - map[y - 1][x] - 1);
    }
    if (y < map.length - 1) {
        search_rec(map, output, x, y + 1, limit - map[y + 1][x] - 1);
    }
}

希望这个逻辑是有道理的,它实际上解决了你想要解决的问题。

【讨论】:

  • 你绝对是个天才。我最大的遗憾是我只能给你一个赞成票和一个复选标记。如果这是 reddit,我会给你金币。
猜你喜欢
  • 2011-04-24
  • 2018-08-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多