diff --git a/题目3.cpp b/题目3.cpp new file mode 100644 index 0000000..0a26ad5 --- /dev/null +++ b/题目3.cpp @@ -0,0 +1,136 @@ +#include +int board[9][9] = {{5, 3, 0, 0, 9, 0, 0, 0, 0}, +{6, 0, 0, 1, 7, 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, 0}, +{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, 2, 5}, +{0, 0, 0, 0, 8, 0, 0, 7, 9}}; + +//ÓÐʲô·½·¨¿ÉÒÔ×öµ½ÊäÈë¶àάÊý×éµÄÒ»Ðе½¶¨ÒåµÄº¯ÊýÀïÃæ +int main(void) +{ + printf("The original Sudoku matrix:\n"); + for(int i=0;i<9;i++) + { + if(i%3==0)printf(" |----------------------|\n"); + for(int j=0;j<9;j++) + { + if(j%3==0)printf(" |"); + if(board[i][j]==0) printf(" ."); + else printf(" %d",board[i][j]); + } + putchar('|'); + putchar('\n'); + } + printf(" |----------------------|\n"); + + //ÅжÏÐÐ + int count1=0; + int storage1; + int judge1=0; + for(int i=0;i<9;i++) + { + for(int n=1;n<10;n++) + { + for(int j=0;j<9;j++) + { + if(board[i][j]==n)count1++; + if(count1>1) + { + judge1=1; + storage1=n; + break; + } + } + count1=0; + //ÅÐ¶Ï + if(judge1) + { + printf("False:Invalid initial Sudoku matrix\n"); + printf("The number %d in the col %d has been used!\n",storage1,i+1); + break; + } + + } + if(judge1)break; + } + + //ÅжÏÁÐ + int count2=0; + int storage2; + int judge2=0; + for(int i=0;i<9;i++) + { + for(int n=1;n<10;n++) + { + for(int j=0;j<9;j++) + { + if(board[j][i]==n)count2++; + if(count2>1) + { + judge2=1; + storage2=n; + break; + } + } + count2=0; + //ÅÐ¶Ï + if(judge2) + { + printf("False:Invalid initial Sudoku matrix\n"); + printf("The number %d in the list %d has been used!\n",storage2,i+1); + break; + } + + } + if(judge2)break; + } + + //ÅжÏÇø¿é + int count3=0; + int storage3; + int judge3=0; + for(int col=0;col<3;col++) + { + for(int list=0;list<3;list++) + { + for(int n=1;n<10;n++) + { + for(int i=3*col;i<3*col+3;i++) + { + for(int j=3*list;j<3*list+3;j++) + { + if(board[i][j]==n)count3++; + if(count3>1) + { + judge3=1; + storage3=n; + break; + } + } + if(judge3)break; + } + count3=0; + } + //ÅÐ¶Ï + if(judge3) + { + printf("False:Invalid initial Sudoku matrix\n"); + if(col==0) + printf("The number %d in the block %d has been used!\n",storage3,1+list); + else if(col==1) + printf("The number %d in the block %d has been used!\n",storage3,4+list); + else + printf("The number %d in the block %d has been used!\n",storage3,7+list); + break; + } + if(judge3)break; + } + if(judge3)break; + } + if(!judge1&&!judge2&&!judge3)printf("True:Valid initial Sudoku matrix!\n"); + return 0; +}