From e9f8bbec44fe1ebea83b7920cb85431960bc6425 Mon Sep 17 00:00:00 2001 From: pns4qt5fo <2085596522@qq.com> Date: Sun, 29 Oct 2023 19:34:58 +0800 Subject: [PATCH] ADD file via upload --- part 3.c | 144 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 144 insertions(+) create mode 100644 part 3.c diff --git a/part 3.c b/part 3.c new file mode 100644 index 0000000..fdd46d2 --- /dev/null +++ b/part 3.c @@ -0,0 +1,144 @@ +#include +#include +#include + +//void shuchu(int a[9][9]) { +// for (int i = 0; i < 9; i++) { +// if (i % 3 == 0) { +// printf("|"); +// for (int j = 0; j < 23; j++) { +// printf("-"); +// } +// printf("|\n"); +// } +// for (int j = 0; j < 9; j++) { +// if (j % 3 == 0) +// printf("| "); +// printf("%d ", a[i][j]); +// if (j == 8) +// printf("| "); +// } +// printf("\n"); +// if (i == 8) { +// printf("|"); +// for (int j = 0; j < 23; j++) { +// printf("-"); +// } +// printf("|"); +// +// } +// } +//} + +//void MakeRand(int arr[], int count) { +// srand((unsigned int)time(NULL)); +// for (int i = 0; i < count; i++) { +// int num = i + rand() % (count - i); +// int temp = arr[i]; +// arr[i] = arr[num]; +// arr[num] = temp; +// } +//} + +void shudu(int a[9][9]) { + int flag = 0; + int z = 0; + int i, j, k, l; + int count[10] = {0}; + int m[9]; + for (i = 0; i < 9; i++) { + int count[10] = {0}; + for (j = 0; j < 9; j++) { + if (a[i][j] != 0) + count[a[i][j]]++; + } + for (j = 0; j < 9; j++) { + if (count[j + 1] >= 2) { + flag = 1; + z = j + 1; + break; + } + } + if (flag == 1) + break; + } + if (flag == 1) { + printf("False:Invalid initialSudoku matrix\nThe number %d in the row %d has been used!,z,j"); + return ; + } + for (i = 0; i < 9; i++) { + int count[10] = {0}; + for (j = 0; j < 9; j++) { + if (a[j][i] != 0) + count[a[j][i]]++; + } + for (j = 0; j < 9; j++) { + if (count[j + 1] >= 2) { + flag = 1; + z = j + 1; + break; + } + } + if (flag == 1) + break; + } + if (flag == 1) { + printf("FalseInvalid initial Sudoku matrix\nThe number %d in the col %d has been used!", z, j); + return ; + } + for (i = 0; i < 3; i++) { + for (j = 0; j < 3; j++) { + for (k = 0; k < 3; k++) { + for (l = 0; l < 3; l++) { + m[k * 3 + l] = a[j * 3 + k][i * 3 + l]; + } + } + for (k = 0; k < 9; k++) + count[k] = 0; + for (k = 0; k < 9; k++) { + if (m[k] != 0) + count[m[k]]++; + } + for (k = 0; k < 9; k++) { + if (count[k] >= 2) { + flag = 1; + z = k; + break; + } + } + if (flag == 1) + break; + } + if (flag == 1) + break; + } + if (flag == 1) { + printf("False:Invalid initial Sudoku matrix!\nThe number %d in the block %d has been used!", z, j); + return ; + } else + printf("Ture:Valid initial sudoku matrix"); +} + +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"); + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + printf("%d ", board0[i][j]); + } + printf("\n"); + } + printf("\n"); + shudu(board0); + + +}