これは、迷路アルゴリズムの変更についてです。与えられたアルゴリズムのわずかな変更
どういう意味ですか? 0と1で塗りつぶされた2次元配列が得られます。ここで0は「渡すことができません」を意味し、1は「可能にする」を意味します。 そのアルゴリズムは、xからyへの道を見つけます(既知の例:catからmouse)。 これは、以下のアルゴリズムが行っていることです。
は、入力として、我々は得た:
{1, 0, 0,},
{1, 1, 0},
{0, 1, 1} };
そして出力:
- 変更の開始と終了位置(このアルゴリズムの開始:私はいくつかの小さな物事を変えたい
(0,0) // ressembles the coordinates of the 1 in top left corner (1,0) // ressembles the 1 under the first 1 I just explained (1,1) // ... (2,1) (2,2)
を左下と右下に終わります) - 私は私の左下から始まり、右上から終わります。
- このアルゴリズムは上下に動かすことができます - 私は上下に移動したいだけです。私はかなり確信しているが、私はそれをコーディングする方法がわからない、実行する必要がどのような変更
:1の場合 )の問題があると思われる:どういうわけか
public List<Coordinate> solve() {
return getMazePath(0, 0, new Stack<Coordinate>());
}
、I 2番目のゼロを0-1とする必要がありますが、xとyの宣言にアクセスできない場合はどうすればよいですか?私は本当に0-1が左上ではなく左下でスタートさせると信じています、そうですか?
2)については、列の変更も必要です。 +1の代わりに-1が必要です、そうですか?
テキストの壁のために申し訳ありませんは、私は本当に短いそれを維持しようとしたが、私は失敗しているように見える:とにかくP 私は誰かが変化することなく、この^^
アルゴリズムを読むことを願っています:
import java.util.Arrays;
import java.util.*;
final class Coordinate {
private final int x;
private final int y;
public Coordinate(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
}
public class Alg {
private final int[][] maze;
public Alg(int[][] maze) {
if (maze == null) {
throw new NullPointerException("The input maze cannot be null");
}
if (maze.length == 0) {
throw new IllegalArgumentException("The size of maze should be greater than 0");
}
this.maze = maze;
}
public List<Coordinate> solve() {
return getMazePath(0, 0, new Stack<Coordinate>());
}
private List<Coordinate> getMazePath(int row, int col, Stack<Coordinate> stack) {
assert stack != null;
stack.add(new Coordinate(row, col));
if ((row == maze.length - 1) && (col == maze[0].length - 1)) {
Coordinate[] coordinateArray = stack.toArray(new Coordinate[stack.size()]);
return Arrays.asList(coordinateArray);
}
for (int j = col; j < maze[row].length; j++) {
if ((j + 1) < maze[row].length && maze[row][j + 1] == 1) {
return getMazePath(row, j + 1, stack);
}
if ((row + 1) < maze.length && maze[row + 1][col] == 1) {
return getMazePath(row + 1, col, stack);
}
}
return Collections.emptyList();
}
public static void main(String[] args) {
int[][] m = { {1, 0, 0,},
{1, 1, 0},
{0, 1, 1} };
Alg maze = new Alg(m);
for (Coordinate coord : maze.solve()) {
System.out.println("("+coord.getX() + "," + coord.getY()+")");
}
}
}
重複: [1](http://stackoverflow.com/q/37480866/522444)、 [2](http://stackoverflow.com/q/37482819/522444)、 [3]( http://stackoverflow.com/q/37480866/522444)、 [4](http://stackoverflow.com/q/37485751/522444)、[5](http://stackoverflow.com/q/37490334/522444)。 –