该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.util.*;
public class CheckSudokuSolution
{
public static void main(String[] args)
{
int[][] usersSolution = new int[9][9];
inputUsersSolution(usersSolution);
System.out.println(isSolutionValid(usersSolution) ? "Valid solution" : "Invalid solution");
}
public static void inputUsersSolution(int[][] list)
{
Scanner input = new Scanner(System.in);
for(int x = 0; x < list.length; x++)
{
for(int y = 0; y < list[x].length; y++)
{
list[x][y] = input.nextInt();
}
}
}
public static boolean isSolutionValid(int[][] list)
{
boolean isValid = true;
int[] temp = new int[9];
/*
for(int x = 0; x < 9; x++)
{
for(int y = 0; y < 9; y++)
{
temp[y] = list[x][y];
}
isValid = isOneDValid(temp);
if(!isValid)
break;
}
*/
/*
if(isValid)
{
for(int x = 0; x < 9; x++)
{
for(int y = 0; y < 9; y++)
{
temp[y] = list[y][x];
}
isValid = isOneDValid(temp);
if(!isValid)
break;
}
}
*/
if(isValid)
{
out:for(int x = 0; x < 2; x++)
{
for(int y = 0; y < 2; y++)
{
for(int z = 0; z < 3; z++)
{
for(int i = 0; i < 3; i++)
{
temp[z * 3 + i] = list[x * 3 + z][y * 3 + i];
}
}
isValid = isOneDValid(temp);
if(!isValid)
break out;
}
}
}
return isValid;
}
public static boolean isOneDValid(int[] list)
{
Arrays.sort(list);
for(int x = 0; x < 9; x++)
{
if(list[x] != x + 1)
return false;
}
return true;
}
}
就是最后验3*3那里出错了;不知道为什么
附上:
一个正确的数独:
9 6 3 1 7 4 2 5 8
1 7 8 3 2 5 6 4 9
2 5 4 6 8 9 7 3 1
8 2 1 4 3 7 5 9 6
4 9 6 8 5 2 3 1 7
7 3 5 9 6 1 8 2 4
5 8 9 7 1 3 4 6 2
3 1 7 2 4 6 9 8 5
6 4 2 5 9 8 1 7 3
检验3*3用的数独:
9 6 3 1 7 4 2 5 8
1 7 8 3 2 5 6 4 9
2 5 4 6 8 9 7 9 1
8 2 1 4 3 7 5 3 6
4 9 6 8 5 2 3 1 7
7 3 5 9 6 1 8 2 4
5 8 9 7 1 3 4 6 2
3 1 7 2 4 6 9 8 5
6 4 2 5 9 8 1 7 3