You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.5 KiB
111 lines
2.5 KiB
3 weeks ago
|
#include<stdio.h>
|
||
|
|
||
|
void valid(int a[9][9]){
|
||
|
int x=1;
|
||
|
int i,j,ii,jj;
|
||
|
for (i = 0; i < 9; i++) {
|
||
|
int count[10] = {0};
|
||
|
for (j = 0; j < 9; j++) {
|
||
|
if (a[i][j]!= 0) {
|
||
|
if (count[a[i][j]] > 0) {
|
||
|
x = 0;
|
||
|
printf("False:Invalid initial Sudoku matrix!\n The number %d in the row %d has been used!\n", a[i][j], i + 1);
|
||
|
return;
|
||
|
}
|
||
|
count[a[i][j]]++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
int y=1;
|
||
|
for (j = 0; j < 9; j++) {
|
||
|
int count[10] = {0};
|
||
|
for (i = 0; i <9; i++) {
|
||
|
if (a[i][j]!= 0) {
|
||
|
if (count[a[i][j]] > 0) {
|
||
|
y = 0;
|
||
|
printf("False:Invalid initial Sudoku matrix!\n The number %d in the col %d has been used!\n", a[i][j], j + 1);
|
||
|
return;
|
||
|
}
|
||
|
count[a[i][j]]++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
int z=1;
|
||
|
for (i = 0; i <9; i += 3) {
|
||
|
for (j = 0; j <9; j += 3) {
|
||
|
int count[10] = {0};
|
||
|
for (ii = i; ii < i + 3; ii++) {
|
||
|
for (jj = j; jj < j + 3; jj++) {
|
||
|
if (a[ii][jj]!= 0) {
|
||
|
if (count[a[ii][jj]] > 0) {
|
||
|
z = 0;
|
||
|
printf("False:Invalid initial Sudoku matrix!\n The number %d in the 3x3 submatrix starting at row %d, col %d has been used!\n", a[ii][jj], i + 1, j + 1);
|
||
|
return;
|
||
|
}
|
||
|
count[a[ii][jj]]++;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
if (x&&y&&z) {
|
||
|
printf("True:Valid initial Sudoku matrix!\n");
|
||
|
}
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
void print(int a[9][9]) {
|
||
|
printf("|-----------------------|\n");
|
||
|
int i,j;
|
||
|
for(i=0;i<9;i++){
|
||
|
printf("| ");
|
||
|
for(j=0;j<9;j++){
|
||
|
printf("%d ",a[i][j]);
|
||
|
if(j==2||j==5) printf("| ");
|
||
|
if(j==8) printf("|\n");
|
||
|
}
|
||
|
if(i==2||i==5) {
|
||
|
int x=22;
|
||
|
printf("|");
|
||
|
while(x>=0) {
|
||
|
printf("-");
|
||
|
x--;
|
||
|
}
|
||
|
printf("|\n");
|
||
|
}
|
||
|
}
|
||
|
printf("|-----------------------|\n");
|
||
|
}
|
||
|
|
||
|
int main (){
|
||
|
int board0[9][9] = {
|
||
|
{5, 3, 0, 0, 7, 0, 0, 0, 0},
|
||
|
{6, 0, 0, 1, 9, 5, 0, 0, 0},
|
||
|
{0, 9, 8, 0, 0, 0, 0, 6, 0},
|
||
|
{8, 0, 0, 0, 6, 0, 0, 0, 3},
|
||
|
{4, 0, 0, 8, 0, 3, 0, 0, 1},
|
||
|
{7, 0, 0, 0, 2, 0, 0, 0, 6},
|
||
|
{0, 6, 0, 0, 0, 0, 2, 8, 0},
|
||
|
{0, 0, 0, 4, 1, 9, 0, 0, 5},
|
||
|
{0, 0, 0, 0, 8, 0, 0, 7, 9}
|
||
|
};
|
||
|
printf("The original Sudoku matrix: \n");
|
||
|
print(board0);
|
||
|
valid(board0);
|
||
|
int board1[9][9] = {
|
||
|
{8, 3, 0, 0, 7, 0, 0, 0, 0},
|
||
|
{6, 0, 0, 1, 9, 5, 0, 0, 0},
|
||
|
{0, 9, 8, 0, 0, 0, 0, 6, 0},
|
||
|
{8, 0, 0, 0, 6, 0, 0, 0, 3},
|
||
|
{4, 0, 0, 8, 0, 3, 0, 0, 1},
|
||
|
{7, 0, 0, 0, 2, 0, 0, 0, 6},
|
||
|
{0, 6, 0, 0, 0, 0, 2, 8, 0},
|
||
|
{0, 0, 0, 4, 1, 9, 0, 0, 5},
|
||
|
{0, 0, 0, 0, 8, 0, 0, 7, 9}
|
||
|
};
|
||
|
printf("The original Sudoku matrix: \n");
|
||
|
print(board1);
|
||
|
valid(board1);
|
||
|
return 0;
|
||
|
}
|