diff --git a/3.c b/3.c new file mode 100644 index 0000000..8df8706 --- /dev/null +++ b/3.c @@ -0,0 +1,104 @@ +#include +void printmat(int board[9][9]) +{ + int i,j; + for (i=0;i<9;i++) + { + if(i%3==0) printf("|-----------------------|\n"); + for(j=0;j<9;j++) + { + if(j%3==0) printf(j==0?"|":" |"); + printf(" %d",board[i][j]); + } + printf(" |\n"); + } + printf("|-----------------------|\n"); + return; +} + +int main() +{ + int board[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 checkmat[9][2][2]={{{0,0},{3,3}}, + {{0,3},{3,6}}, + {{0,6},{3,9}}, + {{3,0},{6,3}}, + {{3,3},{6,6}}, + {{3,6},{6,9}}, + {{6,0},{9,3}}, + {{6,3},{9,6}}, + {{6,6},{9,9}}}; + int i,j,k,p,num; + printf("The original Sudoku matrix\n"); + printmat(board); + + + for (i=0;i<9;i++) + { + int flag[9] = {0,0,0,0,0,0,0,0,0}; + for (j=0;j<9;j++) + { + if (board[i][j]!=0 ) + { + if (flag[board[i][j]-1]==1) + { + printf("False:Invalid initial Sudoku matrix\n"); + printf(" The number %d in the row %d has been used!\n",board[i][j],i+1); + return 0; + } + else flag[board[i][j]-1]=1; + } + } + } + + for (i=0;i<9;i++) + { + int flag[9] = {0,0,0,0,0,0,0,0,0}; + for (j=0;j<9;j++) + { + if (board[j][i]!=0 ) + { + if (flag[board[j][i]-1]==1) + { + printf("False:Invalid initial Sudoku matrix\n"); + printf(" The number %d in the col %d has been used!\n",board[j][i],i+1); + return 0; + } + else flag[board[j][i]-1]=1; + } + } + } + + for (k=0;k<9;k++) + { + int flag[9] = {0,0,0,0,0,0,0,0,0}; + for (i=checkmat[k][0][0];i