次のコードはあなたが説明したことを実行すると思います。最初のノード(0,0)からチェックを開始します。チェックされるノードごとに、ネイバーのベクトルが作成されます。隣接ノードは、パスへの継続として適格なノードです(つまり、テーブル内でより高い値を持つ隣接ノード)。次に、ネイバーごとにパスが複製され、新しいネイバーがチェックされます。これは、チェックされたノードに適格なネイバーがなくなるまで続きます。その時点でパスが出力され、アルゴリズムが終了します。
これを試して:
import java.util.Arrays;
import java.util.Vector;
class Main {
class Coords {
int x;
int y;
Coords(int x, int y) {
this.x = x;
this.y = y;
}
}
int [][] array = { {3,5,1},{6,7,4},{8,2,9}};
Vector<Coords> getNeighbors(Coords coords) {
int x = coords.x;
int y = coords.y;
Vector<Coords> result = new Vector<Coords>();
if (x < array.length - 1) {
if (array[x + 1][y] >= array[x][y])
result.add(new Coords(x + 1, y));
}
if (x > 0) {
if (array[x - 1][y] >= array[x][y])
result.add(new Coords(x - 1, y));
}
if (y < array[x].length - 1) {
if (array[x][y + 1] >= array[x][y])
result.add(new Coords(x, y + 1));
}
if (y > 0) {
if (array[x][y - 1] >= array[x][y])
result.add(new Coords(x, y - 1));
}
if (x < (array.length - 1 ) && (y < array[x].length - 1)) {
if (array[x + 1][y + 1] >= array[x][y])
result.add(new Coords(x + 1, y + 1));
}
if (x < (array.length - 1 ) && (y > 0)) {
if (array[x + 1][y - 1] >= array[x][y])
result.add(new Coords(x + 1, y - 1));
}
if (x > 0 && (y < array[x].length - 1)) {
if (array[x - 1][y + 1] >= array[x][y])
result.add(new Coords(x - 1, y + 1));
}
if (x > 0 && y > 0) {
if (array[x -1][y - 1] >= array[x][y])
result.add(new Coords(x - 1, y - 1));
}
return result;
}
void checkNode(Vector<Integer> path, Coords coords) {
path.add(array[coords.x][coords.y]);
Vector<Coords> neighbors = getNeighbors(coords);
if (neighbors.size() == 0) {
for (Integer i : path) {
System.out.print(i+"\t");
}
System.out.println();
}
for (Coords c : neighbors) {
Vector<Integer> newpath = (Vector<Integer>) path.clone();
checkNode(newpath, c);
}
}
Main() {
System.out.println ("Array: " + Arrays.deepToString(array));
checkNode(new Vector<Integer>(),new Coords(0,0));
}
public static void main(String args[]) {
new Main();
}
}
出力:
Array: [[3, 5, 1], [6, 7, 4], [8, 2, 9]]
3 6 8
3 6 7 9
3 6 7 8
3 5 7 9
3 5 7 8
3 5 6 8
3 5 6 7 9
3 5 6 7 8
3 7 9
3 7 8
また、サンプル出力にはないパス3、6、8も表示されます。