簡體   English   中英

在JS中使用多維數組查找路徑

[英]Finding a path using multi-dimensional arrays in js

我有一個存儲在多維數組中的xy網格。 xy網格中的每個點都有一個值。

例:

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

假設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

現在,為了舉例說明,假設上面的布局是地圖,就像棋盤一樣。 對於初學者來說,無需關注網格上每個點的值,假設我們想知道“游戲片段”可以從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步。 我們不知道方向,只是運動的潛力。

這很容易弄清楚。 我仍在嘗試的部分是:您如何基於網格的值知道點a處的物體是否可以到達點i。 0表示沒有增強,但1或2則需要1或2或任何額外的運動才能到達.....如何計算出地圖上的每個點? 同樣,不知道哪個方向或路徑-是否最佳。

澄清:想象一下一個棋盤。 每個方形棋盤都有一個值,該值表示到達該位置需要多少個EXTRA移動點。 給定的棋子,根本不使用國際象棋規則,可以在任何方向上移動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中,但是他想知道每個方塊是否可以在該方塊中顯示-布爾值,是或否。 僅需9個正方形,這很容易,但是隨着網格的擴大,復雜性也隨之增加。 我當然可以手動進行-他能達到X4Y4嗎? 是。 但是,以編程方式,我如何得到這個?

EDIT3:網格的大小是沒有意義的,我才意識到。 確實是范圍的大小。 例如,如果運動范圍是5,我只需要找出每個方向上五個正方形的正方形的可行性即可。 這樣就簡化了一點。

EDIT4:我認為我有一個更好的主意。 看一下最外圈5。 大於0嗎? 那就不要。 下一個最外圈(4出)。 大於1? 不,下一個最外圈。 大於2? 那就不要。 等等,這行得通還是會導致錯誤的結果?

首選使用js或jQuery的答案(甚至只是正確的方向),但是即使您可以通過邏輯進行某種工作,我也可以將其轉換為js或jquery。

我認為您想做的是一種基本的搜索,在每次搜索中您都要檢查周圍的正方形。 用這個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);
    }
}

希望這種邏輯是有意義的,並且實際上是在解決您要解決的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM