Springerproblem - ein bisschen anders

districon

Bekanntes Mitglied
Hallo hier ist zuerst mal die Aufgabe:Bildschirmfoto 2021-06-24 um 16.09.32.png
Und hier mein gesamter Code:
Java:
public class JavaSpringerProblems {
    public static int[][] realsol;
    public static int countglob;
    public static int[][] solve(boolean[][] board, int startRow, int startCol) throws IllegalArgumentException {
        if (checkboard(board) == false || checkPoint(board,startRow, startCol) == false) {
            throw new IllegalArgumentException();
        }
        int[][] sol = new int[board.length][];
        for (int i = 0 ; i < board.length; i++) {
            sol[i] = new int[board[i].length];
        }
        for (int row = 0; row < sol.length; row++) {
            for (int col = 0; col < sol[row].length; col++) {
                if (board[row][col] == false) {
                    sol[row][col] = 0;
                }
                else {
                    sol[row][col] = 0;
                }
            }
        }
        realsol = sol;
        int counter = 0;
        for (int row = 0; row < sol.length; row++) {
            for (int col = 0; col < sol[row].length; col++) {
                if (board[row][col] == true) {
                    counter++;
                }
            }
        }
        countglob = counter;
        int[] x_move = new int[]{2, 1, -1, -2, -2, -1, 1, 2};
        int[] y_move = new int[]{1, 2, 2, 1, -1, -2, -2, -1};

        sol[startRow][startCol] = 1;

        if(springtour(sol, startRow, startCol,1,x_move,y_move, board, counter)) {
            return realsol;
        }

        return null;
    }
    public static boolean springtour(int[][] sol, int i, int j, int step_count, int[] x_move, int[] y_move, boolean[][] board, int counter) {
        if (step_count == counter) {
            return true;
        }
        for(int k=0; k<8; k++) {
            int nextI = i+x_move[k];
            int nextJ = j+y_move[k];

            if (isValid(nextI, nextJ, sol, board)) {
                sol[nextI][nextJ] = step_count;
                realsol[nextI][nextJ] = step_count;

                if (springtour(sol, nextI, nextJ, step_count+1, x_move, y_move,board, counter)) {
                    return true;
                }
                    sol[nextI][nextJ] = 0;
                realsol[nextI][nextJ] = 0;
            }
        }
        return false;
    }
    public static boolean isValid(int i, int j, int[][] sol, boolean[][] board) {
        if (checkPoint(board,i,j) && i >= 0 && j >= 0 && sol[i][j] == -1 && board[i][j] != false) {
            return true;
        }
        return false;
    }
    public static boolean checkboard(boolean[][] board) {
        for (int i0 = 0; i0 < board.length; i0++) {

            boolean[] dim1 = board[i0];

            if (dim1 == null) {
                return false;
            }
        } if (board.length == 0) {
            return false;
        }
        return true;
    }
    public static boolean checkPoint(boolean[][] board, int i, int j) {
        try {
            boolean test = board[i][j];

        } catch (IndexOutOfBoundsException a) {
            return false;
        }
        return true;
    }
Man hätte es bestimmt auch ein wenig anders machen können..Das Backtracking findet hauptsächlich in springtour() statt. Irgendwie returnt aber solve immer noch null. Mache ich was mit meinem Backtracking falsch?
Danke
 

Neue Themen


Oben