问题
I am currently attempting to create a Java program that reads 81 integers (1-9) into an 9 X 9 matrix, and then tests to see if that matrix is a solution to a Sudoku puzzle. The parameters for a Sudoku solution are as follows: each number (1-9) must be represented in every row, column, and 3x3 square without any repetition in these areas. I have written a method to verify that the parameters are met for all rows and columns, however, I am struggling with coming up with an algorithm to verify the squares. Here is what I have so far:
import java.util.*;
public class SudokuCheck
{
public static boolean sudokuCheck(int[][] s)
{
for(int row=0;row<9;row++)
for(int col=0;col<8;col++)
if(s[row][col]==s[row][col+1]){
return false;}
//Verifies rows
for(int col2=0;col2<9;col2++)
for(int row2=0;row2<8;row2++)
if (s[row2][col2]==s[row2+1][col2])
return false;
//verifies columns
return true;
}
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
int[][] solution = new int [9][9];
System.out.println("Enter the values of a 9 X 9 Sudoku solution");
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
solution[i][j]=input.nextInt();
//read values into matrix
if(sudokuCheck(solution)==true)
System.out.println("The entered 9 X 9 grid is a solution to a Sudoku puzzle.");
else
System.out.println("The entered 9 X 9 grid is not a solution to a Sudoku puzzle.");
}
}
回答1:
This can probably be optimized but following your approach
// row checker
for(int row = 0; row < 9; row++)
for(int col = 0; col < 8; col++)
for(int col2 = col + 1; col2 < 9; col2++)
if(s[row][col]==s[row][col2])
return false;
// column checker
for(int col = 0; col < 9; col++)
for(int row = 0; row < 8; row++)
for(int row2 = row + 1; row2 < 9; row2++)
if(s[row][col]==s[row2][col])
return false;
// grid checker
for(int row = 0; row < 9; row += 3)
for(int col = 0; col < 9; col += 3)
// row, col is start of the 3 by 3 grid
for(int pos = 0; pos < 8; pos++)
for(int pos2 = pos + 1; pos2 < 9; pos2++)
if(s[row + pos%3][col + pos/3]==s[row + pos2%3][col + pos2/3])
return false;
回答2:
I hope it helps you
public static void main(String[] args) throws Exception {
int[][] f = {{4,2,9,8,1,3,5,6,7}, {5,1,6,4,7,2,9,3,8}, {7,8,3,6,5,9,2,4,1},
{6,7,2,1,3,4,8,5,9}, {3,9,5,2,8,6,1,7,4}, {8,4,1,7,9,5,6,2,3},
{1,5,8,3,6,7,4,9,2}, {9,3,4,5,2,8,7,1,6}, {2,6,7,9,4,1,3,8,5}};
System.out.println(Arrays.toString(f));
System.out.println(checkBlock(f, 2, 2));
}
public static boolean checkBlock(int[][] f, int z, int s) throws Exception {
boolean[] mark = new boolean[9];
boolean res = true;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
int v = f[z * 3 + i][s * 3 + j];
if (v == 0) {
res = false;
}
if (mark[v - 1]) {
throw new Exception();
}
mark[v - 1] = true;
}
}
return res;
}
回答3:
public class SudokuVerify {
public static void main(String[] args) {
int[][] arr = { { 5, 3, 4, 6, 7, 8, 9, 1, 2 },
{ 6, 7, 2, 1, 9, 5, 3, 4, 8 }, { 1, 9, 8, 3, 4, 2, 5, 6, 7 },
{ 8, 5, 9, 7, 6, 1, 4, 2, 3 }, { 4, 2, 6, 8, 5, 3, 7, 9, 1 },
{ 7, 1, 3, 9, 2, 4, 8, 5, 6 }, { 9, 6, 1, 5, 3, 7, 2, 8, 4 },
{ 2, 8, 7, 4, 1, 9, 6, 3, 5 }, { 3, 4, 5, 2, 8, 6, 1, 7, 9 } };
boolean flag = true;
int ExpectedVal = (1 ^ 2 ^ 3 ^ 4 ^ 5 ^ 6 ^ 7 ^ 8 ^ 9);
int ActualVal = 0;
//---># Checking for row Validation**
for (int row = 0; row < 9; row++) {
ActualVal = 0;
for (int col = 0; col < 9; col++) {
ActualVal ^= arr[row][col];
}
if (ActualVal != ExpectedVal) {
flag = false;
break;
}
}
//---># **Checking for col Validation**
if (flag) {
for (int row = 0; row < 9; row++) {
ActualVal = 0;
for (int col = 0; col < 9; col++) {
ActualVal ^= arr[col][row];
}
if (ActualVal != ExpectedVal) {
flag = false;
break;
}
}
}
//---># **Checking for inside box Validation**
if (flag) {
for (int i = 0; i < 9; i += 3) {
if (flag) {
for (int j = 0; j < 9; j += 3) {
ActualVal = 0;
for (int ii = i; ii < (i + 3); ii++) {
for (int jj = j; jj < (j + 3); jj++) {
ActualVal ^= arr[ii][jj];
}
System.out.println();
}
if (ActualVal != ExpectedVal) {
flag = false;
break;
}
}
} else
break;
}
}
if (flag)
System.out.println("Passes");
else {
System.out.println("Faied");
}
}
}
来源:https://stackoverflow.com/questions/34076389/java-sudoku-solution-verifier