数独解决方案检查器

时间:2017-03-01 19:49:50

标签: java artificial-intelligence puzzle sudoku

我有一个Java程序来检查数据是否有效,我有两种方法,第一种是检查所有列,子网格,行的总和,并判断是否为45,第二种是检查如果所有数据的总和是405,如果是,所以数独有效,所以我正在寻找一个反例,在输入中我有一个有效的数据,但程序告诉它是无效的,所以这是代码:

public class test_checker {
    static final boolean valide=true;
    static final boolean non_valide=false;

    // verify every sub-grid if it is valid
        static boolean check_subgrid(int a[][],int ei,int ej){
            int sum=0;
            for(int i=ei;i<ei+3;i++){           
                for(int j=ej;j<ej+3;j++){
                    sum=sum+a[j][i];
                }
            }
            if(sum!=45) return non_valide;
            else return valide;
        }
        //verify a sudoku by sum of every row & column & sub-grid
        static boolean Checker1(int a[][]){
            int sum=0;
            //check row
            for(int i=0;i<9;i++){
                sum=0;
                for(int j=0;j<9;j++){
                    sum=sum+a[i][j];
                }
                if(sum!=45) return non_valide;

            }
            //check column
            for(int i=0;i<9;i++){
                sum=0;
                for(int j=0;j<9;j++){
                    sum=sum+a[j][i];
                }
                if(sum!=45) return non_valide;
            }       
            //check sub-grid
            for(int i=0;i<9;i=i+3){
                for(int j=0;j<3;j=j+3){
                    if(check_subgrid(a,i,j)==non_valide) return non_valide;
                }
            }
            return valide;
        }
        //verify by sum of all sudoku  
        static boolean Checker2(int a[][]){
            int sum=0;
            for(int i=0;i<9;i++){           
                for(int j=0;j<9;j++){
                    sum=sum+a[i][j];
                }
            }   
            if(sum!=405) return non_valide;
            else return valide;
        }


    public static void main(String[] args) {
        int [][] sudoku =
            {
                {1,2,3,4,5,6,7,8,9},
                {4,5,6,7,8,9,1,2,3},
                {7,8,9,1,2,3,4,5,6},
                {3,1,2,9,7,8,6,4,5},
                {6,4,5,3,1,2,9,7,8},
                {9,7,8,6,4,5,3,1,2},
                {2,3,1,5,6,4,8,9,7},
                {5,6,4,8,9,7,2,3,1},
                {8,9,7,2,3,1,5,6,4}
            };
        if(Checker1(sudoku)) System.out.println("it's valide (checker1)!");
        else System.out.println("it's not valide !");
        if(Checker2(sudoku)) System.out.println("it's valide (checker2) !");
        else System.out.println("it's not valide !");

    }

}

2 个答案:

答案 0 :(得分:2)

正如@John Bollinger所说

  

你应该问的是,是否有一个无效的9乘9网格   然而,确实符合您的标准。

我想到的最简单的两个案例是:

{5,5,5,5,5,5,5,5,5},
{5,5,5,5,5,5,5,5,5},
{5,5,5,5,5,5,5,5,5},
{5,5,5,5,5,5,5,5,5},
{5,5,5,5,5,5,5,5,5},
{5,5,5,5,5,5,5,5,5},
{5,5,5,5,5,5,5,5,5},
{5,5,5,5,5,5,5,5,5},
{5,5,5,5,5,5,5,5,5}

和你的例子,你会把0放在1而不是1 和10而不是9。确保每个字段在行,列和子网格中是不同的,并且间隔为[1,9]。

答案 1 :(得分:1)

对于checker2,您只需反转两行的两个值即可。在这里我颠倒了第一行和第二行中的4和1,显然它是一个无效的数独,但是checker2会给你一个肯定的结果

            {4,2,3,4,5,6,7,8,9},
            {1,5,6,7,8,9,1,2,3},
            {7,8,9,1,2,3,4,5,6},
            {3,1,2,9,7,8,6,4,5},
            {6,4,5,3,1,2,9,7,8},
            {9,7,8,6,4,5,3,1,2},
            {2,3,1,5,6,4,8,9,7},
            {5,6,4,8,9,7,2,3,1},
            {8,9,7,2,3,1,5,6,4}