あるインデックスから別のインデックスへの最短パスを見つける必要があります。
インデックスを進めるには、そのインデックスは、その隣のインデックスと同じ値を持つ「隣人」でなければなりません。すべての可能なパスを再帰的に計算し、すべてを行列に入れる関数を作成する必要があります。
これまでのところ、どこでフラッド フィル アルゴリズムを使用し、何らかの方法で各パスをカウントするかを考えています。
わかりましたので、これを行う方法を理解しようとしてかなり長い間ここに座っていましたが、これまでのところ、このアイデアに行き着きました。各インデックスで最初のノードからの距離を表示する複製配列を作成します。私がこれを正しく行っているかどうかわからないので、少し助けが必要です.これは私が書いたものです:
public static int[][] createdistancematrix(int[][] map, int i, int j) {
int[][] temp = new int[map.length][map[0].length];
map[i][j] = 0;
int count = 0;
filldistance(temp, i, j,count);
return temp;
}
public static int[][] filldistance(int[][] temp, int i, int j,int count) {
if ((i<temp.length) && (i>=0) && (j<temp.length) && (j>=0)) {
temp[i][j] = count;
count++;
//recursive call for north east west south.
filldistance(temp, i-1,j, count);
filldistance(temp, i+1,j, count);
filldistance(temp, i,j-1, count);
filldistance(temp, i,j+1, count);
}
return temp;
}