From 08f8569870e15953eee2fbf9cac9a3bf83971670 Mon Sep 17 00:00:00 2001 From: m7wzyfpis <1040766652@qq.com> Date: Sun, 5 Nov 2023 09:47:42 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=BB=E5=8A=A13?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 3.cpp | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 3.cpp diff --git a/3.cpp b/3.cpp new file mode 100644 index 0000000..8b5b40a --- /dev/null +++ b/3.cpp @@ -0,0 +1,82 @@ +#include +#include +#include + +int checkMatrix(int matrixArr[9][9], int isPrintInfo){ + int rowCnt[9][10]={0}; + int colCnt[9][10]={0}; + int bloCnt[9][10]={0}; + for(int i=0;i<9;i++){ + for(int j=0;j<9;j++){ + if(matrixArr[i][j]!=0){ + rowCnt[i][matrixArr[i][j]]++; + if(rowCnt[i][matrixArr[i][j]]>1){ + if(isPrintInfo){ + printf("\nFalse:Invalid initial Sudoku matrix!"); + printf("\nThe number %d in the row %d has been used!",matrixArr[i][j],i+1); + } + return 0; + } + colCnt[j][matrixArr[i][j]]++; + if(colCnt[j][matrixArr[i][j]]>1){ + if(isPrintInfo){ + printf("\nFalse:Invalid initial Sudoku matrix!"); + printf("\nThe number %d in the col %d has been used!",matrixArr[i][j],j+1); + } + return 0; + } + bloCnt[i/3*3+j/3][matrixArr[i][j]]++; + if(bloCnt[i/3*3+j/3][matrixArr[i][j]]>1){ + if(isPrintInfo){ + printf("\nFalse:Invalid initial Sudoku matrix!"); + printf("\nThe number %d in the block %d has been used!",matrixArr[i][j],i/3*3+j/3+1); + } + return 0; + } + } + } + } + if(isPrintInfo){ + printf("\nTrue:Valid initial Sudoku matrix!"); + } + return 1; +} +void matrixOutput(int matrixArr[9][9]) +{ + for (int i = 0; i < 9; i++) + { + for (int j = 0; j < 9; j++) + { + printf("%d", matrixArr[i][j]); + if (j != 8) + printf(" "); + } + if (i != 8) + 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}}; + 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"); + matrixOutput(board1); + checkMatrix(board1,1); + return 0; + }