diff --git a/program3.c b/program3.c new file mode 100644 index 0000000..55a7d35 --- /dev/null +++ b/program3.c @@ -0,0 +1,151 @@ +#include +int main() +{ + int board[9][9]={{0,1,2,0,7,0,0,0,3}, + {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 a[9][9];//设置不同的区块 + int q,p,r=0; + /*************定义块*******************/ + for(q=0;q<3;q++)//section 1 + { + for(p=0;p<3;p++) + { + a[0][r++]=board[q][p]; + } + } + r=0; + for(q=0;q<3;q++)//section 2 + { + for(p=3;p<6;p++) + { + a[1][r++]=board[q][p]; + } + }r=0; + for(q=0;q<3;q++)//section 3 + { + + for(p=6;p<9;p++) + { + a[2][r++]=board[q][p]; + } + } r=0; + for(q=3;q<6;q++)//section 4 + { + + for(p=0;p<3;p++) + { + a[3][r++]=board[q][p]; + } + }r=0; + for(q=3;q<6;q++)//section 5 + { + for(p=3;p<6;p++) + { + a[4][r++]=board[q][p]; + } + } r=0; + for(q=3;q<6;q++)//section 6 + { + for(p=6;p<9;p++) + { + + a[5][r++]=board[q][p]; + } + } r=0; + for(q=6;q<9;q++)//section 7 + { + for(p=0;p<3;p++) + { + a[6][r++]=board[q][p]; + } + } r=0; + for(q=6;q<9;q++)//section 8 + { + for(p=3;p<6;p++) + { + a[7][r++]=board[q][p]; + } + } r=0; + for(q=6;q<9;q++)//section 9 + { + for(p=6;p<9;p++) + { + a[8][r++]=board[q][p]; + } + } + //设置区块结束 ,将每个区块的数转移为每一行的数,再判断行是否重复 + + int i,j,k,t=0,h=0; + int n=0; + printf("The original Sudoku matrix:\n"); + /*************检索行*******************/ + for(i=0;i<9;i++)//先判断行是否重复 + { + for(j=0;j<9;j++) + { + for(k=0;k<9;k++)//对第j行的数进行判断在该行内是否重复,若重复则跳出循环 + { + if(board[i][j]==board[i][k]&&j!=k&&board[i][j]!=0) + { + n=board[i][j]; + goto flagone;//在某一行重复出现 + } + } + } + } + /*************检索列*******************/ + for(h=0;h<9;h++)//判断列是否重复 + { + for(j=0;j<9;j++) + { + for(k=0;k<9;k++) + { + if(board[j][h]==board[k][h]&&j!=k&&board[j][h]!=0) + { + n=board[j][h]; + goto flagtwo;//在某一列重复出现 + } + } + } + } + /*************检索块*******************/ + for(i=0;i<9;i++)//先判断行(块)是否重复 + { + for(j=0;j<9;j++) + { + for(k=0;k<9;k++)//对第j行(块)的数进行判断在该行(块)内是否重复,若重复则跳出循环 + { + if(a[i][j]==a[i][k]&&j!=k&&a[i][j]!=0) + { + n=a[i][j]; + goto flagthree;//在某一行(块)重复出现 + } + } + } + /*************结果*******************/ + if(n==0) + { + printf("True:Invalid initial Sudoku matrix!\n"); + } + }if(n!=0){ + flagone: + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the col %d has been used!\n",n,i+1); + return 0; + flagtwo: + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the row %d has been used!\n",n,h+1); + return 0; + flagthree: + printf("False:Invalid initial Sudoku matrix!\n"); + printf("The number %d in the block %d has been used!\n",n,i+1); + return 0; + } +}