[英]Why does the output for my program always start with two consecutive zeros even though I wrote a code that doesn't produce zeros or repeated numbers?
[英]Sudoku java, wrote a program but doesn't pick up on repeated numbers
我正在编写一个 Java 程序来验证一组 9x9 的二维整数数组是否是数独谜题的可能解决方案。 问题是我编写的程序没有处理一些并排的整数。 例如,我在 5x4 的 8 旁边放了一个 8,它仍然返回 true。 以下是按列检查行和 3 × 3 块的方法。先谢谢你
public class Sudoku{
private static boolean sudoku= true;
public static void main(String[] args){
int input[][]= {{1,8,4,9,6,3,7,2,5},
{5,6,2,7,4,8,3,1,9},
{3,9,7,5,1,2,8,6,4},
{2,3,9,6,5,7,1,4,8},
{7,5,6,8,8,4,2,9,3},//repeated "8" here
{4,1,8,2,3,9,6,5,7},
{9,4,1,3,7,6,5,8,2},
{6,2,3,8,9,5,4,7,1},
{8,7,5,4,2,1,9,3,6}};
RowXColumn(input);
if(sudoku == true)
Blocks(input);
else{
}
System.out.println("True or false, is this a sudoku puzzle solution: " + sudoku);
}
public static void RowXColumn(int [][]b){
for(int row= 0; row < a.length; row++){
for(int col=0; col< a[row].length; col++){//checks rows
int b= a[row][col];
for(int x= row+1; x< a.length; x++){
if(b == a[x][col])
sudoku= false;
else
continue;
}
for(int y= col+1; y< a[row].length; y++){//checks columns
if(b == a[row][y])
sudoku= false;
else
continue;
}
}
}
}
public static void Blocks(int [][]b){//checks 3x3 blocks
for(int d= 0; d< (b.length/3); d++){
for(int e= 0; e< (b[d].length/3); e++){
for(int d1= (d*3); d1< (b.length); d1+=3){
for(int e1= (e*3); e1< (b[e].length); e1+=3){
int bu= b[d1][e1];
for(int co=d1; co < 3; co++){
for(int col= e1 +1; e1< 3; e1++){
if(bu == b[co][e1])
sudoku= false;
else
continue;
}
}
}
}
}
}
}
}
经过一些研究,我想我解决了自己的问题。 另外,很抱歉对问题的错误解释
public class Sudoku{
private static boolean sudoku= true;
public static void main(String[] args){
int input[][]= {{1,8,4,9,6,3,7,2,5},
{5,6,2,7,4,8,3,1,9},
{3,9,7,5,1,2,8,6,4},
{2,3,9,6,5,7,1,4,8},
{7,5,6,1,8,4,2,9,3},
{4,1,8,2,3,9,6,5,7},
{9,4,1,3,7,6,5,8,2},
{6,2,3,8,9,5,4,7,1},
{8,7,5,4,2,1,9,3,6}};
RowXColumn(input);
if(sudoku == true)
Blocks(input);
System.out.println("True or false, is this a sudoku puzzle solution: " + sudoku);
}
public static void RowXColumn(int [][]a){
for(int row=0; row < a.length; row++){
for(int col=0; col< a[row].length; col++){
for(int x= row+1; x< a.length; x++){//Checks columns
if(a[row][col] == a[x][col])
sudoku = false;
}
for(int y= col+1; y< a[row].length; y++){//Checks rows
if(a[row][col] == a[row][y])
sudoku = false;
}
}
}
}
public static void Blocks(int [][]b){
for(int row= 0; row< (b.length); row+= 3){
for(int col= 0; col< (b[row].length); col+= 3){
for(int p= row; p< (row+2); p++){
for(int p1= col; p1< (col+2); p1++){//picks out value to compare
for(int comp= p1+1 ; comp < 3-p1; comp++){//compares
if(b[p][p1] == b[p][comp])
sudoku= false;
}
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.