私は現在、迷路を通して単一の解決策を見つけるJavaプログラムを作成しようとしています。私はいつもどのように上記の方法を把握しようとしている迷路を解決するための方法を作り始める前に、そうJava - ユニークなソリューションで迷路を作成
private void Create (int x, int y, int val) {
int[] perm = randPerm(4);
m[x][y] ^= val;
for (int i=0; i<4; ++i) {
int p = perm[i];
if (m[x+DX[p]][y+DY[p]] == 15) {
m[x][y] ^= TWO[p];
Create(x+DX[p], y+DY[p], TWO[p^2]);
}
}
}
:これを行うには、私はユニークなソリューションを持っている迷路を作成し、作成する方法を使用していますユニークなパスを持つ迷路を作成します(p^2のように が使用されています)。では、上記の方法はどのように機能しますか?
private int[][] m; // maze representation
private int rows; // number of rows in the maze
private int cols; // number of columns in the maze
private final static byte[] TWO = { 1, 2, 4, 8, 16};
private final static byte[] DX = { 0,+1, 0,-1};
private final static byte[] DY = {-1, 0,+1, 0};
private boolean done; // used in finding a single solution.
private long count; // used in finding the number of solutions.
private Random r; // for generating random integers.
public Maze (int nr, int nc, int seed) {
r = new Random(seed);
rows = nr; cols = nc;
m = new int[nr+2][nc+2];
for (int r=1; r<=nr; ++r)
for (int c=1; c<=nc; ++c)
m[r][c] = 15;
for (int r=0; r<nr+2; ++r)
m[r][0] = m[r][nc+1] = 16;
for (int c=0; c<nc+2; ++c)
m[0][c] = m[nr+1][c] = 16;
Create(nr/2+1, nc/2+1, 0);
}
private int[] randPerm(int n) {
int[] perm = new int[n];
for (int k=0; k<n; ++k) perm[k] = k;
for (int k=n; k>0; --k) {
int rand = r.nextInt(k);
int t = perm[rand]; perm[rand] = perm[k-1]; perm[k-1] = t;
}
return(perm);
}
関連するすべてのコードを表示できますか?例えば、 'm'と' TWO'変数はあなたが与えたスニペットには定義されていません。 –
私はそれを更新してより多くの方法を示しました。 – Chase
'int [] perm = randPerm(4);も知っておく必要があります。私は推測をしましたが、確かに知っているといいですね。 – NAMS