0

これについてどうすればよいかまったくわかりませんが、Java の AI パスファインディング システムに配列を使用しており、値を並べ替える必要があります。私が生成している配列は次のとおりです。

int[][] paths = new int[Settings.GAME_COLS][Settings.GAME_ROWS]; // Just creating an Array that has room for every tile in the Game, so the Array can be used like this: paths[x][y]
int tempF = 0;

tempF = Math.abs(14 + (((aiX + 1) - playerX) + ((aiY - 1) - playerY)));
paths[aiX + 1][aiY - 1] = tempF;
tempF = Math.abs(14 + (((aiX + 1) - playerX) + ((aiY + 1) - playerY)));
paths[aiX + 1][aiY + 1] = tempF;
tempF = Math.abs(14 + (((aiX - 1) - playerX) + ((aiY + 1) - playerY)));
paths[aiX - 1][aiY + 1] = tempF;
tempF = Math.abs(14 + (((aiX - 1) - playerX) + ((aiY - 1) - playerY)));
paths[aiX - 1][aiY - 1] = tempF;
tempF = Math.abs(10 + (((aiX + 1) - playerX) + (aiY - playerY)));
paths[aiX + 1][aiY    ] = tempF;
tempF = Math.abs(10 + (((aiX - 1) - playerX) + (aiY - playerY)));
paths[aiX - 1][aiY    ] = tempF;
tempF = Math.abs(10 + ((aiX - playerX) + ((aiY + 1) - playerY)));
paths[aiX    ][aiY + 1] = tempF;
tempF = Math.abs(10 + ((aiX - playerX) + ((aiY - 1) - playerY)));
paths[aiX    ][aiY - 1] = tempF;

それはすべて完全に機能し、必要な情報を含む配列を見つけて生成しますが、配列に追加された「tempF」値に基づいて配列をソートする必要があります。これを行う簡単な方法はありますか?

4

2 に答える 2

2

多次元配列は配列のセットにすぎないため、 Arrays.sort配列の配列をその場でソートする通常の方法を使用できます。

于 2012-09-04T18:28:27.343 に答える
0
for(int k=0;k<N;k++)  // loop for relaxation between row-column sorts
{
    for(int i=0;i<N;i++)
    {
       //row-wise sortings(for all rows)
       Arrays.sort(your_array[i]); 
    }

      for ( int c = 0 ; c < N ; c++ )
      {
         for( int d = 0 ; d < N ; d++ )               
         transposed_your_array[d][c] = your_array[c][d];         
      }  

    for(int i=0;i<N;i++)
    {
       //column-wise sortings(for all columns)
       Arrays.sort(transposed_your_array[i]);
    }

    for ( int c = 0 ; c < N ; c++ )
    {
        for( int d = 0 ; d < N ; d++ )               
        your_array[d][c] = transposed_your_array[c][d];         
    }  //getting original array from transpose


}
//There are actually N*N 1-D(N-element)arrays in a N x N matrix
//Every sort on a row-array alters the bounding column-arrays too(N of them)


//a worked example:
//8  3  8
//11 8  3
//6  12 6

//output:
//  this(0,0) is smallest element
//  ^
//  |
//  3  6  8
//  3  8  11
//  6  8  12 ---->this (2,2) is biggest element

//some other examples:
//10 8 8 
//11 4 7 
//9 5 3 
//
//3 5 9 ----> this 9 could be in place of 8 down there but this 
//4 7 10      program sorts rows first then sorts columns
//8 8 11      you can interleave them for better resolution sort(slower)
于 2012-09-04T18:38:13.150 に答える