0
で迷路のパスを辿りだから私は適切に迷路を解くプログラムを持っており、以下の何のように見える多次元整数配列solvedMaze
溶液を格納します。ここで指摘しては、Java
110111
110101
010100
110100
011100
000000
開始および終了である:私は迷路のパスを解決し、リトレースする両方有する
S10111
11010E
010100
110100
011100
000000
コードを以下に示す:
public List<Location> solution() {
int[][] solvedMaze = new int[height][width];
Location current;
PriorityQueue<Location> queue = new PriorityQueue<>(new Comparator<Location>() {
@Override
public int compare(Location a, Location b) {
double distanceA = distance(start, a)/1.5 + distance(a, end);
double distanceB = distance(start, b)/1.5 + distance(b, end);
return Double.compare(distanceA, distanceB);
}
private double distance(Location first, Location second) {
return Math.abs(first.i() - second.i()) + Math.abs(first.j() - second.j());
}
});
queue.add(start);
while ((current = queue.poll()) != null) {
if (solvedMaze[current.i()][current.j()] != 0) {
continue;
}
int mod = 1;
for (Location next : new Location[]{
current.south(), current.west(), current.north(), current.east()
}) {
if (isInMaze(next) && isClear(next)) {
if (solvedMaze[next.i()][next.j()] == 0) {
queue.add(next);
} else {
mod = Math.min(solvedMaze[next.i()][next.j()], mod);
}
}
}
solvedMaze[current.i()][current.j()] = mod;
if (isFinal(current)) {
break;
}
}
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
System.out.print(solvedMaze[i][j]);
}
System.out.println();
}
if (solvedMaze[end.i()][end.j()] != 0) {
List<Location> route = new ArrayList<>();
Location temp = end;
while (!temp.equals(start)) {
route.add(temp);
Location best = null;
int bestNumber = solvedMaze[temp.i()][temp.j()];
for (Location next : new Location[]{
temp.north(), temp.south(), temp.west(), temp.east()
}) {
if (isInMaze(next) && solvedMaze[next.i()][next.j()] != 0) {
if (solvedMaze[next.i()][next.j()] < bestNumber) {
bestNumber = solvedMaze[next.i()][next.j()];
best = next;
}
}
}
assert best != null;
temp = best;
}
route.add(start);
Collections.reverse(route);
return route;
} else {
return null;
}
}
を
Location
は、x座標とy座標を含むクラスであり、start
とend
はロケーションです。何らかの理由で私の出力は常にnull
であり、私はその理由をよく分かりません。単純なprint
のデバッグの後、私はリトレースロジックのsolvedMaze[next.i()][next.j()] < bestNumber
状態が決して入力されないことを発見しました。メソッドの問題点は何ですか?それを解決するためのより良い(より効率的な)方法がありますか?
私は方法全体を含めるように質問を変更しました。それは迷路を解決し、それを歩き回ります。うまくいけば、それは今より明らかにすべきです! – T145
それは役に立ちますか? – T145