数独生成器:java.lang.StackOverflowError

时间:2015-10-13 09:31:26

标签: java stack-overflow sudoku backtracking

这是我生成随机解决的数独板的代码。我使用回溯alghoritm跟踪每个细胞的可能性。有时候(比如10次中的3次)它不起作用并且给我这个 [编辑]我知道这个错误是什么,但我不知道如何解决

  

线程“main”中的异常java.lang.StackOverflowError

这是代码:

我该如何解决这个问题? (checkSquare方法太长,无法在此处发布,但只检查数字在所选方块中是否有效(3x3)

    int [][] board = new int[9][9];
    int [][][] poss = sudoku.creaposs();
    Object[] obj = new Object[2];
    obj[1] = poss;
    obj[0] = board;
    boolean generato;
    do{

    try{
        long start = System.currentTimeMillis();
        obj[0] = creasudo((int[][])obj[0],(int[][][])obj[1],0,0);
        long end = System.currentTimeMillis();
        System.out.println("Tempo trascorso: "+(end-start));
        generato = true;
    }
    catch(Exception exp){
        System.out.println(exp);
        generato = false;
    }
        //sudoku.stampa((int[][]) obj[0]);
    }while(!generato);
    sudoku.stampa((int[][])obj[0]);

方法

public static int[][] creasudo(int[][] board, int[][][] poss,int i, int j){
        if (j<0){
            if (i == 0){
                System.out.println("FINEEE");
                System.exit(-1);

            }
            else
            {j = 8;
            i--;
            }
        }
        if (j>8){
            j=0;
            i++;
        }
        if (i == 9)
            return board;
        int n; 
        Random rand = new Random();
        if (poss[i][j][0] == 0){
            poss[i][j] = new int[9];
            for (n=0;n<9;n++){
                poss[i][j][n]=n+1;
            }
            board[i][j]=0;
            return creasudo(board,poss,i,j-1);
        }
        n = rand.nextInt(poss[i][j].length);
        if (!sudoku.checkcol(board,j,poss[i][j][n])|| !sudoku.checkrow(board,i,poss[i][j][n]) ||
                sudoku.checksquare(board,i,j,poss[i][j][n])){
                poss[i][j] = sudoku.removeEl(poss[i][j], poss[i][j][n]);

                return creasudo(board,poss,i,j);
        }
        else{
            board[i][j] = poss[i][j][n];
            return creasudo(board,poss,i,j+1);
        }

    }

public static boolean checkrow(int[][] board, int r, int num){
    int i;
    for (i = 0; i<RIGHE; i++){
        if (board[r][i] == num)
            return false;

    }

    return true;
}

public static boolean checkcol(int[][]board, int c, int num){
    int i;
    for (i = 0; i<RIGHE; i++){
        if (board[i][c] == num)
            return false;
    }

    return true;
}

public static int[]removeEl(int[] array, int n){
    int[] array2;
    int r = 0;
    int i;
    int count = 0;
    for (i = 0; i<array.length; i++){
        if (array[i] != n){
            count ++;
        }
    }
    if (count == 0||(array.length == 1 && array[0] == 0)){
        array2 = new int[1];
        array2[0] = 0;
    }
    else{
        array2 = new int[count];
        for (i = 0; i<array.length; i++){
            if (array[i] != n){
                array2[r] = array[i];
                r++;
            }
        }
    }
    return array2;
}

0 个答案:

没有答案