Hallo,
gerne würde ich ein Rätsel mit dem Ausgang 1 2 3
8 0 5
4 7 6
lösen, so dass der Zielzustand: 1 2 3
4 5 6
7 8 0
zu erreichen. Mein Code nutzt eine Nodeklasse, und verzweiget für jeden Zustand in die möglichen Richtungen. Allerdings kommt die Rekursion nicht so richtig in Gang! Es wird versetzt, zurückgesetzt und dann abgebrochen! Wo liegt der Fehler?
gerne würde ich ein Rätsel mit dem Ausgang 1 2 3
8 0 5
4 7 6
lösen, so dass der Zielzustand: 1 2 3
4 5 6
7 8 0
zu erreichen. Mein Code nutzt eine Nodeklasse, und verzweiget für jeden Zustand in die möglichen Richtungen. Allerdings kommt die Rekursion nicht so richtig in Gang! Es wird versetzt, zurückgesetzt und dann abgebrochen! Wo liegt der Fehler?
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package FU_2;
import java.util.ArrayList;
/**
*
* @author RK
*/
public class L_E2_3 {
int[][] ziel = {{1, 2, 3}, {4, 5, 6}, {7, 8, 0}};
int[][] ausgang = {{1, 2, 3}, {8, 0, 5}, {4, 7, 6}};
Node root = new Node();
public Node g_erster(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 1; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i - 1][j];
matrix[i - 1][j] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
System.out.println("Neue Matrix errechnet");
ausgabe(matrix);
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.content == matrix) {
doppelt = true;
Node neu = new Node();
neu = null;
return neu;
} else {
n = n.parent;
}
}
System.out.println("Ein neuer Knoten wird generiert " + matrix);
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
Node neu = new Node();
neu = null;
return neu;
}
public Node g_zweiter(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i + 1][j];
matrix[i + 1][j] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
System.out.println("Neue Matrix errechnet");
ausgabe(matrix);
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.content == matrix) {
doppelt = true;
Node neu = new Node();
neu = null;
return neu;
} else {
n = n.parent;
}
}
System.out.println("Ein neuer Knoten wird generiert " + matrix);
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
Node neu = new Node();
neu = null;
return neu;
}
public Node g_dritter(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 0; i < 3; i++) {
for (int j = 1; j < 3; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i][j - 1];
matrix[i][j - 1] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
System.out.println("Neue Matrix errechnet");
ausgabe(matrix);
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.content == matrix) {
doppelt = true;
Node neu = new Node();
neu = null;
return neu;
} else {
n = n.parent;
}
}
System.out.println("Ein neuer Knoten wird generiert " + matrix);
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
Node neu = new Node();
neu = null;
return neu;
}
public Node g_vierter(int[][] matrix, Node n) {
int generiert = 0;
for (int i = 1; i < 3; i++) {
for (int j = 0; j < 2; j++) {
if (matrix[i][j] == 0) {
int zwischen = matrix[i][j + 1];
matrix[i][j + 1] = 0;
matrix[i][j] = zwischen;
i = 3;
j = 3;
generiert = 1;
System.out.println("Neue Matrix errechnet");
ausgabe(matrix);
}
}
}
if (generiert == 1) {
boolean doppelt = false;
while (n != null) {
if (n.content == matrix) {
doppelt = true;
Node neu = new Node();
neu = null;
return neu;
} else {
n = n.parent;
}
}
System.out.println("Ein neuer Knoten wird generiert " + matrix);
Node neu = new Node();
neu.parent = n;
neu.content = matrix;
return neu;
}
Node neu = new Node();
neu = null;
return neu;
}
public Node insert(Node n) {
if (n != null) {
// Node v = new Node();
// n.eins = v;
n.eins = g_erster(n.content, n);
if (n.eins != null && !abbruch(n.eins)) {
insert(n.eins);
}
if (abbruch(n.eins)) {
return n.eins;
}
Node w = new Node();
n.zwei = w;
n.zwei = g_zweiter(n.content, n);
if (n.zwei != null && !abbruch(n.zwei)) {
insert(n.zwei);
}
if (abbruch(n.zwei)) {
return n.zwei;
}
Node x = new Node();
n.drei = x;
n.drei = g_dritter(n.content, n);
if (n.drei != null && !abbruch(n.drei)) {
insert(n.drei);
}
if (abbruch(n.drei)) {
return n.drei;
}
Node y = new Node();
n.vier = y;
n.vier = g_vierter(n.content, n);
if (n.vier != null && !abbruch(n.vier)) {
insert(n.vier);
}
if (abbruch(n.vier)) {
return n.vier;
}
}
return null;
}
public boolean abbruch(Node n) {
if (n != null && n.content == ziel) {
return true;
} else {
return false;
}
}
public void ausgabe(int[][] matrix){
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
System.out.println("____________________");
System.out.println();
}
public static void main(String[] args) {
// TODO code application logic here
L_E2_3 p = new L_E2_3();
p.root.content = p.ausgang;
p.root.parent = new Node();
p.root.parent = null;
Node v = new Node();
v = p.root;
Node resultat = new Node();
resultat = p.insert(p.root);
while (resultat != null) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(resultat.content[i][j] + " ");
}
System.out.println();
}
System.out.println("____________________");
System.out.println();
resultat = resultat.parent;
}
}
}
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package FU_2;
/**
*
* @author RK
*/
public class Node {
Node eins;
Node zwei;
Node drei;
Node vier;
Node parent;
int[][] content;
}