From eaf97b755cc09bb5c759d9c4be7f5aa918d29d8b Mon Sep 17 00:00:00 2001 From: pjc97uv2f <1291955673@qq.com> Date: Sat, 9 Nov 2024 01:10:11 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=E7=AC=AC=E4=B8=89=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 第三题 | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 第三题 diff --git a/第三题 b/第三题 new file mode 100644 index 0000000..d4bdcee --- /dev/null +++ b/第三题 @@ -0,0 +1,73 @@ +#include +#include +#define SIZE 9 + +void printMatrix(char matrix[SIZE][SIZE]) { + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + printf("%c ", matrix[i][j]); + } + printf("\n"); + } +} + +bool isValidSudoku(char matrix[SIZE][SIZE]) { + bool rowCheck[SIZE][SIZE] = {false}; + bool colCheck[SIZE][SIZE] = {false}; + bool blockCheck[SIZE][SIZE] = {false}; + + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + char c = matrix[i][j]; + if (c == '.') continue; + + int num = c - '1'; + + if (rowCheck[i][num]) { + printf("不是数独矩阵\n"); + printf("第 %d 行出现重复数字 %d。\n", i + 1, num + 1); + return false; + } + rowCheck[i][num] = true; + + if (colCheck[j][num]) { + printf("不是数独矩阵\n"); + printf("第 %d 列出现重复数字 %d。\n", j + 1, num + 1); + return false; + } + colCheck[j][num] = true; + + int blockIndex = (i / 3) * 3 + (j / 3); + if (blockCheck[blockIndex][num]) { + printf("不是数独矩阵\n"); + printf("第 %d 宫出现重复数字 %d。\n", blockIndex + 1, num + 1); + return false; + } + blockCheck[blockIndex][num] = true; + } + } + + printf("是数独矩阵\n"); + return true; +} + +int main() { + char matrix[SIZE][SIZE] = { + {'5', '3', '.', '.', '7', '.', '.', '.', '.'}, + {'6', '.', '.', '1', '9', '5', '.', '.', '.'}, + {'.', '9', '8', '.', '.', '.', '.', '6', '.'}, + {'8', '.', '.', '.', '6', '.', '.', '.', '3'}, + {'4', '.', '.', '8', '.', '3', '.', '.', '1'}, + {'7', '.', '.', '.', '2', '.', '.', '.', '6'}, + {'.', '6', '.', '.', '.', '.', '2', '8', '.'}, + {'.', '.', '.', '4', '1', '9', '.', '.', '5'}, + {'.', '.', '.', '.', '8', '.', '.', '7', '9'} + }; + + printf("原始数独矩阵:\n"); + printMatrix(matrix); + + isValidSudoku(matrix); + + return 0; +} \ No newline at end of file