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.

97 lines
2.5 KiB

#include <stdio.h>
int f[9] = {0};
int t1[9][9] = {0}, t2[9][9] = {0}, t3[9][9] = {0};
int board[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}};
// int board[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}};
void f1()
{
printf("|");
for (int i = 0; i < 23; i++)
{
printf("-");
}
printf("|\n");
}
void operate()
{
for (int i = 0; i < 9; i++)
{
if (i % 3 == 0)
{
f1();
}
printf("| ");
for (int j = 0; j < 9; j++)
{
if (j % 3 == 0 && j)
{
printf("| ");
}
printf("%d ", board[i][j]);
}
printf("|\n");
}
f1();
}
void judge()
{
for (int i = 0; i < 9; i++)
{
for(int j = 0; j < 9; j++)
{
if (board[i][j])
{
t1[i][board[i][j] - 1]++;
t2[j][board[i][j] - 1]++;
t3[(i / 3) * 3 + j / 3][board[i][j] - 1]++;
}
}
}
for (int i = 0; i < 9; i++)
{
for (int num = 0; num < 9; num++)
{
if (t1[i][num] > 1)
{
printf("False:Invalid initial Sudoku matrix!\n");
printf(" The number %d in the row %d has been used!", num + 1, i + 1);
return;
}
else if (t2[i][num] > 1)
{
printf("False:Invalid initial Sudoku matrix!\n");
printf(" The number %d in the col %d has been used!", num + 1, i + 1);
return;
}
else if (t3[i][num] > 1)
{
printf("False:Invalid initial Sudoku matrix!\n");
printf(" The number %d in the block %d has been used!", num + 1, i + 1);
return;
}
}
}
printf("True:Valid initial Sudoku matrix!");
}
int main()
{
printf("The original Sudoku matrix:\n");
operate();
judge();
}