数独求解器回溯算法

时间:2014-03-05 18:34:22

标签: java sudoku backtracking solver

我正在尝试用Java编写数独求解器。我的程序正在创建随机板,然后它正在尝试解决该板。

为此,我编写了检查列,行和框的函数,并下载了回溯求解算法的实现。

我已经分析了这个实现,我认为它应该可行,但事实并非如此。它始终回归说找不到解决方案。

我也尝试自己实现,但结果总是一样的。

请帮助我!

package sudoku;

import java.util.Random;

public class Game {
    int[][] board = new int[9][9];
Random rand = new Random();

public Game(){
    for(int y = 0; y<9; y++)
        for(int x =0; x<9; x++)
            board[x][y] = -1;

    fill(40);
    System.out.println("STARTING BOARD:\n");
    draw();

    System.out.println("\n SOLVED BOARD:");
    if (solve(0,0))    // solves in place
        draw();
    else
        System.out.println("NO SOLUTION!");
}

private void fill(int numbeOfKnownValues){
    int counter=0;

    while(counter != numbeOfKnownValues){
        int Xindex = rand.nextInt((9 - 1) + 0) + 0; //look for the x index in board
        int Yindex = rand.nextInt((9 - 1) + 0) + 0; //look for the y index in board
        int val = rand.nextInt((9 - 1) + 1) + 1;

        if(check(Xindex, Yindex, val)) {
            board[Xindex][Yindex] = val;
        } else continue;

        counter++;
    }
}

private boolean checkColumns(int Xindex, int Yindex, int val){
    if(board[Xindex][Yindex]==-1){
        for(int y=0;y<9;y++){ //check columns
            if(board[Xindex][y]==val) return false;
        }
    } else return false;
    return true;
}

private boolean checkRows(int Xindex, int Yindex, int val){
    if(board[Xindex][Yindex]==-1){
        for(int x=0;x<9;x++){ //check columns
            if(board[x][Yindex]==val) return false;
        }
    } else return false;
    return true;
}

private boolean checkSquares(int Xindex, int Yindex, int val){
    int square_Xindex = (Xindex / 3)*3; 
    int square_Yindex = (Yindex / 3)*3;
    for (int k = 0; k < 3; ++k) // box
        for (int m = 0; m < 3; ++m)
            if (val == board[square_Xindex+k][square_Yindex+m])
                return false;

    return true;
}

private boolean check(int Xindex, int Yindex, int val){
    if(checkColumns(Xindex, Yindex, val) && checkRows(Xindex, Yindex, val) && checkSquares(Xindex, Yindex, val)) return true;
    return false;
}

public boolean solve(int i, int j) {
    if (i == 9) {
        i = 0;
        if (++j == 9)
            return true;
    }

    if (board[i][j] != -1)  // skip filled cells
        return solve(i+1,j);

    for (int val = 1; val <= 9; val++) {
        if (check(i,j,val)) {
            board[i][j] = val;
            if (solve(i+1,j))
                return true;
        }
    }
    board[i][j] = -1; // reset on backtrack
    return false;
}

public void draw(){
    for (int i = 0; i < 9; ++i) {
        if (i % 3 == 0)
            System.out.println(" -----------------------");
        for (int j = 0; j < 9; ++j) {
            if (j % 3 == 0) System.out.print("| ");

            System.out.print(board[i][j] == -1 ? " " : Integer.toString(board[i][j]));
            System.out.print(' ');
        }
        System.out.println("|");
    }
    System.out.println(" -----------------------");
}

}

0 个答案:

没有答案