数独解决方案检查器
Sudoku solution checker
我在 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 !");
}
}
正如@John Bollinger 所说
you should be asking is whether there is an invalid 9 by 9 grid that
nevertheless does satisfy your criteria.
我想到的最简单的两种情况是:
{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
和 10 而不是 9。确保每个字段在行、列和子网格中都是不同的,并且间隔为 [1,9]。
对于 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}
我在 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 !");
}
}
正如@John Bollinger 所说
you should be asking is whether there is an invalid 9 by 9 grid that nevertheless does satisfy your criteria.
我想到的最简单的两种情况是:
{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 和 10 而不是 9。确保每个字段在行、列和子网格中都是不同的,并且间隔为 [1,9]。
对于 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}