3D Box Filler BAcktracking

Status
Nicht offen für weitere Antworten.

Max246Sch

Mitglied
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;
}
}
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
P Frage zu Rekursion und Backtracking Java Basics - Anfänger-Themen 2
districon Backtracking Java Basics - Anfänger-Themen 2
districon Backtracking Java Basics - Anfänger-Themen 14
districon Dynamisch Programmierung/Backtracking/Memoization Java Basics - Anfänger-Themen 3
districon Backtracking funktioniert nicht ganz Java Basics - Anfänger-Themen 3
V Backtracking und Rekursion Java Basics - Anfänger-Themen 15
G Subset sum problem mit Backtracking Java Basics - Anfänger-Themen 18
O Backtracking Java Basics - Anfänger-Themen 5
C Rekursives Backtracking beim Spiel Peg Java Basics - Anfänger-Themen 22
A Backtracking Java Basics - Anfänger-Themen 56
R Backtracking Java Basics - Anfänger-Themen 1
V Feld sortieren mit Backtracking Java Basics - Anfänger-Themen 1
A Sudoku mit Backtracking lösen Java Basics - Anfänger-Themen 3
L Sudoku Backtracking Pseudocode Java Basics - Anfänger-Themen 3
N Backtracking - Labyrinth/Irrgarten Java Basics - Anfänger-Themen 14
I Backtracking Schach Java Basics - Anfänger-Themen 5
L Magisches Quadrat und Backtracking Java Basics - Anfänger-Themen 19
M Backtracking/Solve Methode Java Basics - Anfänger-Themen 7
P Labyrinth, Backtracking, verzögerte Anzeige Java Basics - Anfänger-Themen 15
D Sudoku lösen mit Backtracking Java Basics - Anfänger-Themen 20
E backtracking und Induktionsprinzip Java Basics - Anfänger-Themen 2
D Backtracking Waage Problem Java Basics - Anfänger-Themen 23
N Backtracking Solohalma Java Basics - Anfänger-Themen 2
W Backtracking und Frustration Java Basics - Anfänger-Themen 6
X Sudoku Backtracking Java Basics - Anfänger-Themen 6
J Solitaire via Backtracking Java Basics - Anfänger-Themen 7
A Backtracking - kennt Java keine Rücksprungmarken? Java Basics - Anfänger-Themen 15
G Backtracking mit globaler Variable Java Basics - Anfänger-Themen 5
M BackTracking Java Basics - Anfänger-Themen 22
J Backtracking Algorithmus Java Basics - Anfänger-Themen 16

Ähnliche Java Themen

Neue Themen


Oben