Der folgende Code soll per backtracking eine Box mit den gegebenen Steinen befüllt werden und das ERgebnis zurückgegeben werden. Leider funktioniert das nicht, Seht ihr meinen Fehler? Es kommt keine ausgabe
public enum Main {
;
public static void main(final String[] args) {
final int boxsize = 3;
final int anzahlshapes = 6;
final int rotation = 1;
final String[][][] box = new String[boxsize][boxsize][boxsize]; // 3D-Array für die Box
box[boxsize / 2][boxsize / 2][boxsize / 2] = "G";
final Form[] formes = new Form[anzahlshapes];
formes[0] = new Form(1, 3, 3);
formes[1] = new Form(1, 3, 3);
formes[2] = new Form(1, 1, 3);
formes[3] = new Form(1, 1, 2);
formes[4] = new Form(1, 1, 2);
formes[5] = new Form(1, 1, 1);
Main.solve(box, formes, 0);
}
public static void solve(final String[][][] box, final Form[] formes, final int currentIndex) {
// Basisfall: Wenn alle Formen platziert wurden, drucken Sie die Lösung oder tun Sie etwas damit
if (boxisfull(box)) {
Main.print(box);
return;
}
// Versuchen Sie, die aktuelle Form an verschiedenen Positionen und Rotationen zu platzieren
for (int x = 0; x < box.length; x++) {
for (int y = 0; y < box[0].length; y++) {
for (int z = 0; z < box[0][0].length; z++) {
for (int rotation = 1; rotation <= 6; rotation++) {
if (formes[currentIndex].canbeplaced(x, y, z, box)) {
formes[currentIndex].place(x, y, z, box, currentIndex + 1);
solve(box, formes, currentIndex + 1);
formes[currentIndex].remove(x, y, z, box);
}
}
}
}
}
}
private static void print(final String[][][] box) {
for (int z = 0; z < box[0][0].length; z++) {
for (int y = 0; y < box[0].length; y++) {
for (int x = 0; x < box.length; x++) {
System.out.print(box[x][y][z] + "\t");
}
System.out.println();
}
System.out.println();
}
System.out.println("----------");
}
public static boolean boxisfull(String[][][] box) {
for (int x = 0; x < box.length; x++) {
for (int y = 0; y < box[0].length; y++) {
for (int z = 0; z < box[0][0].length; z++) {
if (box[x][y][z] == null) {
return false; // Es gibt mindestens eine leere Stelle
}
}
}
}
return true;
}
}
class Form {
int xpos, ypos, zpos;
int xlength, ylength, zlength;
int ix, iy, iz;
char type;
int rotation = 1;
public Form(final int xlength, final int ylength, final int zlength) {
this.xlength = xlength;
this.ylength = ylength;
this.zlength = zlength;
ix = xlength;
iy = ylength;
iz = zlength;
if (xlength == ylength && ylength == zlength)
type = 'w';
else
type = 'q';
}
public void place(final int x, final int y, final int z, final String[][][] box, final int index) {
// Platzieren Sie die Form basierend auf den aktuellen Abmessungen
for (int i = 0; i < this.xlength; i++) {
for (int j = 0; j < this.ylength; j++) {
for (int k = 0; k < this.zlength; k++) {
// box[x + i][y + j][z + k] = Integer.toString(index);
box[x + i][y + j][z + k] = Integer.toString(index);
}
}
}
}
public void remove(final int x, final int y, final int z, final String[][][] box) {
for (int i = 0; i < this.xlength; i++) {
for (int j = 0; j < this.ylength; j++) {
for (int k = 0; k < this.zlength; k++) {
box[x + i][y + j][z + k] = null;
}
}
}
}
public void rotate() {
if (7 > this.rotation && 0 < this.rotation) {
switch (rotation) {
case 1:
xlength = this.ix;
ylength = this.iy;
zlength = this.iz;
break;
case 2:
xlength = this.iy;
ylength = this.iz;
zlength = this.ix;
break;
case 3:
xlength = this.iz;
ylength = this.ix;
zlength = this.iy;
break;
case 4:
xlength = this.ix;
ylength = this.iz;
zlength = this.iy;
break;
case 5:
xlength = this.iy;
ylength = this.ix;
zlength = this.iz;
break;
case 6:
xlength = this.iz;
ylength = this.iy;
zlength = this.ix;
break;
}
rotation++;
} else {
rotation = 1; // Zurücksetzen auf den Anfang, wenn rotation 6 erreicht.
this.rotate();
}
}
public boolean canbeplaced(final int x, final int y, final int z, final String[][][] box) {
for (int i = 0; i < this.xlength; i++) {
if (i + x >= box.length) {
return false;
}
for (int j = 0; j < this.ylength; j++) {
if (j + y >= box[0].length) {
return false;
}
for (int k = 0; k < this.zlength; k++) {
if (z + k >= box[0][0].length || null != box[x + i][y + j][z + k]) {
return false;
}
}
}
}
return true;
}
}
public enum Main {
;
public static void main(final String[] args) {
final int boxsize = 3;
final int anzahlshapes = 6;
final int rotation = 1;
final String[][][] box = new String[boxsize][boxsize][boxsize]; // 3D-Array für die Box
box[boxsize / 2][boxsize / 2][boxsize / 2] = "G";
final Form[] formes = new Form[anzahlshapes];
formes[0] = new Form(1, 3, 3);
formes[1] = new Form(1, 3, 3);
formes[2] = new Form(1, 1, 3);
formes[3] = new Form(1, 1, 2);
formes[4] = new Form(1, 1, 2);
formes[5] = new Form(1, 1, 1);
Main.solve(box, formes, 0);
}
public static void solve(final String[][][] box, final Form[] formes, final int currentIndex) {
// Basisfall: Wenn alle Formen platziert wurden, drucken Sie die Lösung oder tun Sie etwas damit
if (boxisfull(box)) {
Main.print(box);
return;
}
// Versuchen Sie, die aktuelle Form an verschiedenen Positionen und Rotationen zu platzieren
for (int x = 0; x < box.length; x++) {
for (int y = 0; y < box[0].length; y++) {
for (int z = 0; z < box[0][0].length; z++) {
for (int rotation = 1; rotation <= 6; rotation++) {
if (formes[currentIndex].canbeplaced(x, y, z, box)) {
formes[currentIndex].place(x, y, z, box, currentIndex + 1);
solve(box, formes, currentIndex + 1);
formes[currentIndex].remove(x, y, z, box);
}
}
}
}
}
}
private static void print(final String[][][] box) {
for (int z = 0; z < box[0][0].length; z++) {
for (int y = 0; y < box[0].length; y++) {
for (int x = 0; x < box.length; x++) {
System.out.print(box[x][y][z] + "\t");
}
System.out.println();
}
System.out.println();
}
System.out.println("----------");
}
public static boolean boxisfull(String[][][] box) {
for (int x = 0; x < box.length; x++) {
for (int y = 0; y < box[0].length; y++) {
for (int z = 0; z < box[0][0].length; z++) {
if (box[x][y][z] == null) {
return false; // Es gibt mindestens eine leere Stelle
}
}
}
}
return true;
}
}
class Form {
int xpos, ypos, zpos;
int xlength, ylength, zlength;
int ix, iy, iz;
char type;
int rotation = 1;
public Form(final int xlength, final int ylength, final int zlength) {
this.xlength = xlength;
this.ylength = ylength;
this.zlength = zlength;
ix = xlength;
iy = ylength;
iz = zlength;
if (xlength == ylength && ylength == zlength)
type = 'w';
else
type = 'q';
}
public void place(final int x, final int y, final int z, final String[][][] box, final int index) {
// Platzieren Sie die Form basierend auf den aktuellen Abmessungen
for (int i = 0; i < this.xlength; i++) {
for (int j = 0; j < this.ylength; j++) {
for (int k = 0; k < this.zlength; k++) {
// box[x + i][y + j][z + k] = Integer.toString(index);
box[x + i][y + j][z + k] = Integer.toString(index);
}
}
}
}
public void remove(final int x, final int y, final int z, final String[][][] box) {
for (int i = 0; i < this.xlength; i++) {
for (int j = 0; j < this.ylength; j++) {
for (int k = 0; k < this.zlength; k++) {
box[x + i][y + j][z + k] = null;
}
}
}
}
public void rotate() {
if (7 > this.rotation && 0 < this.rotation) {
switch (rotation) {
case 1:
xlength = this.ix;
ylength = this.iy;
zlength = this.iz;
break;
case 2:
xlength = this.iy;
ylength = this.iz;
zlength = this.ix;
break;
case 3:
xlength = this.iz;
ylength = this.ix;
zlength = this.iy;
break;
case 4:
xlength = this.ix;
ylength = this.iz;
zlength = this.iy;
break;
case 5:
xlength = this.iy;
ylength = this.ix;
zlength = this.iz;
break;
case 6:
xlength = this.iz;
ylength = this.iy;
zlength = this.ix;
break;
}
rotation++;
} else {
rotation = 1; // Zurücksetzen auf den Anfang, wenn rotation 6 erreicht.
this.rotate();
}
}
public boolean canbeplaced(final int x, final int y, final int z, final String[][][] box) {
for (int i = 0; i < this.xlength; i++) {
if (i + x >= box.length) {
return false;
}
for (int j = 0; j < this.ylength; j++) {
if (j + y >= box[0].length) {
return false;
}
for (int k = 0; k < this.zlength; k++) {
if (z + k >= box[0][0].length || null != box[x + i][y + j][z + k]) {
return false;
}
}
}
}
return true;
}
}