From 3e43e969e5c0a76a6ca95d755dd11000660dc47b Mon Sep 17 00:00:00 2001 From: pjywuv8b9 <3314018306@qq.com> Date: Sat, 4 Nov 2023 22:13:05 +0800 Subject: [PATCH] =?UTF-8?q?Add=20=E7=AC=AC=E4=B8=89=E9=A2=98=EF=BC=88guan?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 第三题(guan) | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 第三题(guan) diff --git a/第三题(guan) b/第三题(guan) new file mode 100644 index 0000000..77ab673 --- /dev/null +++ b/第三题(guan) @@ -0,0 +1,70 @@ +#include +int correct(int board[9][9]){ + int col[9]={0},row[9]={0},block[9]={0};//初始化行、列、块数组 + int i,j; + for(i=0;i<9;++i){ + for(j=0;j<9;j++){//遍历 + int index=1<<(board[i][j]-1);//初始位掩码 + int blockindex=i/3*3+j/3;//块 + if(board[i][j]!=0){//需要判断的位置 + if(row[i]&index){//按位与运算 + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the row %d has been used!",board[i][j],i+1); + return 0; + } + row[i] |=index;//只要判断一次不满足就说明不合法 + if(col[j]&index){ + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the rol %d has been used!",board[i][j],j+1); + return 0; + } + col[j] |=index; + if(block[blockindex]&index){ + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the block %d has been used!",board[i][j],blockindex+1); + return 0; + } + block[blockindex] |=index; + } + + + } + } + printf("True:Valid initial Sudoku matrix!\n");//遍历结束 则正确输出 + return 1; + +} +void print(int board[9][9]){//格式化输出函数 + printf("|---|---|---|---|---|---|\n"); + int i=0,j=0,s=0,k=0; + for(i=0;i<9;++i){ + for(j=0;j<9;j++){ + while(j%3==0){ + printf("| "); + break; + } + printf("%d ",board[i][j]); + } + printf("|\n"); + while((i+1)%3==0){ + printf("|---|---|---|---|---|---|\n"); + break; + } + } +} +int main(){ + int board1[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"); + print(board1); + correct(board1); + + return 0; +} \ No newline at end of file