Hi
ich brauche mal eure Hilfe so richitg funktioniert mein Backtracking Algorithmus nicht. Was is falsch ?
Alles anzeigen
ich brauche mal eure Hilfe so richitg funktioniert mein Backtracking Algorithmus nicht. Was is falsch ?
Quellcode
- class Labyrinth{
- final static int[] STEPX = { 0, 1, 0,-1 };
- final static int[] STEPY = { -1, 0, 1, 0 };
- static int schritt;
- final static int OBEN = 0, RECHTS = 1, UNTEN = 2, LINKS = 3;
- static int aktX;
- static int aktY;
- static char[][] labyrinth = {
- {' ','x','x','x','x'},
- {' ',' ',' ','x','x'},
- {'x','x',' ',' ',' '},
- {'x',' ',' ','x',' '},
- {'x','x','x','x',' '}};
- static boolean findeWeg(char[][] lab, int i, int j){
- int n = lab.length;
- schritt = -1;
- while (schritt != LINKS) {
- schritt ++;
- int neuX = i + STEPX[schritt];
- int neuY = j + STEPY[schritt];
- boolean ok = true;
- if (neuX < 0 || neuX >= n) ok = false;
- if (neuY < 0 || neuY >= n) ok = false;
- if (ok && lab[neuX][neuY] !=' ') ok = false;
- if(ok){
- lab[neuX][neuY] = '.';
- if (findeWeg(lab, neuX, neuY)) {
- return true;
- } else {
- return false;
- }
- }
- }//while
- lab[i][j]= ' ';
- return false;
- }//findeWeg
- public static void zeige(char[][]a){
- for (int i = 0; i < a.length; i++) {
- for (int j = 0; j < a.length; j++) {
- System.out.print(a[i][j]+ " ");
- }
- System.out.println();
- }
- }
- public static void main (String[] args){
- zeige(labyrinth);
- boolean b = findeWeg(labyrinth, 0, 0);
- if (b) System.out.println("Weg gefunden");
- else System.out.println("Keinen Weg gefunden");
- zeige(labyrinth);
- }
- }// Klassenende