js中使用多维数组查找路径

shu*_*ath 1 javascript math logic coordinates multidimensional-array

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

例子:

var xy = [
    [0,3,1,1,0],
    [0,0,2,2,1],
    [0,0,1,1,0]
];
Run Code Online (Sandbox Code Playgroud)

假设 var xy 的布局就像 xy 网格(例如,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
Run Code Online (Sandbox Code Playgroud)

现在,为了举例,假设上面的布局是一张地图,就像一个棋盘。对于初学者来说,不关心网格上每个点的值,假设我们想知道“游戏块”可以从 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!
 }
Run Code Online (Sandbox Code Playgroud)

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

这很容易弄清楚。我仍在尝试解决的部分是:如何根据网格的值知道 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
Run Code Online (Sandbox Code Playgroud)

把它想象成一个游戏,其中每个方块代表某种地形劣势。有些路径比其他路径更容易,但其他路径则更直接。你可以采取任何路径到达某个方格,但在移动之前,哪些方格是合法的,哪些是不合法的?所以我们的作品是在 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
Run Code Online (Sandbox Code Playgroud)

所以这个例子中我们的作品又在 X2Y2 中,但他想知道,对于每个方块,他是否可以到达那里 - 布尔值,是或否。只需九个正方形就很容易,但随着网格的增长,复杂性也随之增加。我当然可以手动完成 - 他能到达 X4Y4 吗?是的。但以编程方式,我如何得到这个?

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

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

首选 js 或 jQuery 的答案(甚至只是引导正确的方向),但即使你可以通过逻辑进行工作,我也可以将其转换为 js 或 jquery。

Xym*_*ech 5

我认为你想要做的是一种基本的搜索,在每次迭代中你检查周围的方块。我用这个 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这个逻辑是有意义的,并且它实际上正在解决您想要解决的问题。