Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- int main()
- {
- int i,j,a,array[9][9],athroisma1[9][9],athroisma2[9][9],athroisma3[9][9];
- int sudoku=1;
- int checkrow[9],checkcolumn[9],checkblock[9];
- for(i=0;i<9;i++)
- {
- for(j=0;j<9;j++)
- {
- for(a=0;a<3;a++) /* Arxikopiei me tin timi 0 olous tous pinakes pou xrisimopiounte stin sinartisi main */
- {
- for(a=0;a<3;a++){
- array[i][j]=0;
- athroisma1[i][j]=0;
- athroisma2[i][j]=0;
- athroisma3[i][j]=0;
- checkrow[i]=0;
- checkcolumn[i]=0;
- checkblock[i]=0;
- }
- }
- }
- }
- for (i=0;i<9;i++)
- {
- for (j=0;j<9;j++)
- {
- scanf("%d ",&array[i][j]);
- if (array[i][j]==1)
- { /*metraei poses fores iparxoun oi arithmoi 1-9 se kathe column */
- athroisma1[j][0]++;
- } else if (array[i][j]==2)
- {
- athroisma1[j][1]++;
- } else if (array[i][j]==3)
- {
- athroisma1[j][2]++;
- } else if (array[i][j]==4)
- {
- athroisma1[j][3]++;
- } else if (array[i][j]==5)
- {
- athroisma1[j][4]++;
- } else if (array[i][j]==6)
- {
- athroisma1[j][5]++;
- } else if (array[i][j]==7)
- {
- athroisma1[j][6]++;
- } else if (array[i][j]==8)
- {
- athroisma1[j][7]++;
- } else if (array[i][j]==9)
- {
- athroisma1[j][8]++;
- }
- if (array[i][j]==1)
- { /* metraei poses fores iparxoun oi arithmoi 1-9 se kathe row*/
- athroisma2[i][0]++;
- } else if (array[i][j]==2)
- {
- athroisma2[i][1]++;
- } else if (array[i][j]==3)
- {
- athroisma2[i][2]++;
- } else if (array[i][j]==4)
- {
- athroisma2[i][3]++;
- } else if (array[i][j]==5)
- {
- athroisma2[i][4]++;
- } else if (array[i][j]==6)
- {
- athroisma2[i][5]++;
- } else if (array[i][j]==7)
- {
- athroisma2[i][6]++;
- } else if (array[i][j]==8)
- {
- athroisma2[i][7]++;
- } else if (array[i][j]==9)
- {
- athroisma2[i][8]++;
- }
- }
- }
- for(a=0;a<9;a++){ /*metraei poses fores iparxoun oi arithmoi 1-9 se kathe block */
- for(i=0;i<3;i++){
- for(j=0;j<3;j++){
- if(array[i][j]==a+1)
- {
- athroisma3[0][a]++;
- }
- }
- }
- for(i=0;i<3;i++){
- for(j=3;j<6;j++){
- if(array[i][j]==a+1)
- {
- athroisma3[1][a]++;
- }
- }
- }
- for(i=0;i<3;i++){
- for(j=6;j<9;j++)
- {
- if(array[i][j]==a+1)
- {
- athroisma3[2][a]++;
- }
- }
- }
- for(i=3;i<6;i++)
- {
- for(j=0;j<3;j++)
- {
- if(array[i][j]==a+1)
- {
- athroisma3[3][a]++;
- }
- }
- }
- for(i=3;i<6;i++){
- for(j=3;j<6;j++){
- if(array[i][j]==a+1)
- {
- athroisma3[4][a]++;
- }
- }
- }
- for(i=3;i<6;i++){
- for(j=6;j<9;j++)
- {
- if(array[i][j]==a+1)
- {
- athroisma3[5][a]++;
- }
- }
- }
- for(i=6;i<9;i++){
- for(j=0;j<3;j++){
- if(array[i][j]==a+1){
- athroisma3[6][a]++;
- }
- }
- }
- for(i=6;i<9;i++){
- for(j=3;j<6;j++){
- if(array[i][j]==a+1)
- {
- athroisma3[7][a]++;
- }
- }
- }
- for(i=6;i<9;i++){
- for(j=6;j<9;j++){
- if(array[i][j]==a+1){
- athroisma3[8][a]++;
- }
- }
- }
- }
- for(i=0;i<9;i++){
- for(j=0;j<9;j++)
- {
- if (athroisma1[i][j]>1 || athroisma2[i][j]>1 || athroisma3[i][j]>1){ /* vriskei an iparxi kapios arithmos 1-9 dio fores se kathe row,column kai block */
- sudoku=0;
- }
- }
- }
- if (sudoku == 1)
- {
- printf("OK"); /* elegxei an to sudoku ontos einai sosto kai tiponi to katalilo minima */
- } else {
- printf(" NOT OK, THATS NOT A VALID SUDOKU \n");
- for (i=0;i<9;i++){
- for(j=0;j<9;j++)
- {
- if(athroisma1[i][j]>1){ /*arrayn den einarrayi, vriskei se poies grarraymmes, se poies sthles karrayi se poiarray blocks entopiarrayetarrayi to provlhmarray*/
- checkcolumn[i]=1;
- }
- if(athroisma2[i][j]>1){
- checkrow[i]=1;
- }
- if(athroisma3[i][j]>1){
- checkblock[i]=1;
- }
- }
- }
- printf("Invalid rows: \n "); /*Tiponi se pies rows,columns kai blocks iparxi lathos sto sudoku*/
- for (i=0;i<9;i++)
- {
- if(checkrow[i]==1)
- {
- printf("%d",i+1);
- }
- }
- printf("Invalid columns: \n ");
- for (i=0;i<9;i++){
- if(checkcolumn[i]==1){
- printf(" %d",i+1);
- }
- }
- printf("Invalid blocks: \n");
- for (i=0;i<9;i++)
- {
- if(checkblock[i]==1)
- {
- printf("%d",i+1);
- }
- }
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement