From 69430d57fd12a3f158b89052247ee7eb93c5207c Mon Sep 17 00:00:00 2001 From: pqrug8l6s <2902004813@qq.com> Date: Wed, 6 Nov 2024 11:53:08 +0800 Subject: [PATCH] ADD file via upload --- ONE_T3.cpp | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 ONE_T3.cpp diff --git a/ONE_T3.cpp b/ONE_T3.cpp new file mode 100644 index 0000000..93e3b77 --- /dev/null +++ b/ONE_T3.cpp @@ -0,0 +1,119 @@ +#include + +// 输出矩阵的格式化结果 +void printarr(int arr[9][9]) +{ + int i,j,k,n; +for(n=0;n<=6;n=n+3) +{ + +printf("|-----------------------|\n") ; +for(i=n;i<=n+2;i++) +{ + for(k=0;k<=6;k=k+3) + { + printf("|"); + for(j=k;j<=k+2;j++) + { + printf(" %d",arr[i][j]); + } + printf(" "); + } + printf("|\n"); +} + +} +printf("|-----------------------|\n") ; +} + + +const char* checkSudoku(int matrix[9][9]) +{ + // 检查每一行 + for (int i = 0; i < 9; i++) + { + int seen[10] = {0}; + for (int j = 0; j < 9; j++) + { + int num = matrix[i][j]; + if (num != 0) + { + if (seen[num] != 0) + { + printf("False:Invalid initial Sudoku matrix!\nThe number %d in the row %d has been used!",num,i+1); + return 0; + } + seen[num] = 1; + } + } + } + + // 检查每一列 + for (int j = 0; j < 9; j++) + { + int seen[10] = {0}; + for (int i = 0; i < 9; i++) + { + int num = matrix[i][j]; + if (num != 0) + { + if (seen[num] != 0) + { + printf("False:Invalid initial Sudoku matrix!\nThe number %d in the col %d has been used!",num,j+1); + return 0; + } + seen[num] = 1; + } + } + } + + // 检查每个 3x3 子矩阵 + for (int block = 0; block < 9; block++) + { + int seen[10] = {0}; + int startRow = (block / 3) * 3; + int startCol = (block % 3) * 3; + for (int i = startRow; i < startRow + 3; i++) + { + for (int j = startCol; j < startCol + 3; j++) + { + int num = matrix[i][j]; + if (num != 0) + { + if (seen[num] != 0) + { + printf("False:Invalid initial Sudoku matrix!\nThe number %d in the block %d has been used!",num,block+1); + return 0; + } + seen[num] = 1; + } + } + } + } + + return "True:Valid initial Sudoku matrix!"; +} + +int main() { + int sudokuMatrix[9][9] = { + {3, 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"); + printarr(sudokuMatrix); + + // 判断数独矩阵是否合法 + const char* result = checkSudoku(sudokuMatrix); + printf("%s\n", result); + + return 0; +}