Advertisement
Guest User

Untitled

a guest
Nov 19th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.68 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3.  
  4.  
  5.  
  6.  
  7. int main()
  8.  
  9. {
  10.  
  11.  
  12.     int i,j,a,array[9][9],athroisma1[9][9],athroisma2[9][9],athroisma3[9][9];
  13.     int sudoku=1;
  14.     int checkrow[9],checkcolumn[9],checkblock[9];
  15.  
  16.  
  17.  
  18.     for(i=0;i<9;i++)
  19.     {
  20.         for(j=0;j<9;j++)
  21.         {
  22.             for(a=0;a<3;a++) /* Arxikopiei me tin timi 0 olous tous pinakes pou xrisimopiounte stin sinartisi main */
  23.             {
  24.                 for(a=0;a<3;a++){
  25.                     array[i][j]=0;
  26.  
  27.                     athroisma1[i][j]=0;
  28.  
  29.                     athroisma2[i][j]=0;
  30.  
  31.                     athroisma3[i][j]=0;
  32.  
  33.                     checkrow[i]=0;
  34.  
  35.                     checkcolumn[i]=0;
  36.  
  37.                     checkblock[i]=0;
  38.  
  39.                 }
  40.             }
  41.         }
  42.     }
  43.     for (i=0;i<9;i++)
  44.     {
  45.         for (j=0;j<9;j++)
  46.         {
  47.             scanf("%d ",&array[i][j]);
  48.  
  49.             if (array[i][j]==1)
  50.             {         /*metraei poses fores iparxoun oi arithmoi 1-9 se kathe column */
  51.                 athroisma1[j][0]++;
  52.             } else if (array[i][j]==2)
  53.             {
  54.                 athroisma1[j][1]++;
  55.             } else if (array[i][j]==3)
  56.             {
  57.                 athroisma1[j][2]++;
  58.             } else if (array[i][j]==4)
  59.             {
  60.                 athroisma1[j][3]++;
  61.             } else if (array[i][j]==5)
  62.             {
  63.                 athroisma1[j][4]++;
  64.             } else if (array[i][j]==6)
  65.             {
  66.                 athroisma1[j][5]++;
  67.             } else if (array[i][j]==7)
  68.             {
  69.                 athroisma1[j][6]++;
  70.             } else if (array[i][j]==8)
  71.             {
  72.                 athroisma1[j][7]++;
  73.             } else if (array[i][j]==9)
  74.             {
  75.                 athroisma1[j][8]++;
  76.             }
  77.              if (array[i][j]==1)
  78.              {         /* metraei poses fores iparxoun oi arithmoi 1-9 se kathe row*/
  79.                 athroisma2[i][0]++;
  80.             } else if (array[i][j]==2)
  81.             {
  82.                 athroisma2[i][1]++;
  83.             } else if (array[i][j]==3)
  84.             {
  85.                 athroisma2[i][2]++;
  86.             } else if (array[i][j]==4)
  87.             {
  88.                 athroisma2[i][3]++;
  89.             } else if (array[i][j]==5)
  90.             {
  91.                 athroisma2[i][4]++;
  92.             } else if (array[i][j]==6)
  93.             {
  94.                 athroisma2[i][5]++;
  95.             } else if (array[i][j]==7)
  96.             {
  97.                 athroisma2[i][6]++;
  98.             } else if (array[i][j]==8)
  99.             {
  100.                 athroisma2[i][7]++;
  101.             } else if (array[i][j]==9)
  102.             {
  103.                 athroisma2[i][8]++;
  104.             }
  105.     }
  106.     }
  107.     for(a=0;a<9;a++){ /*metraei poses fores iparxoun oi arithmoi 1-9 se kathe block */
  108.     for(i=0;i<3;i++){
  109.         for(j=0;j<3;j++){
  110.             if(array[i][j]==a+1)
  111.             {
  112.                 athroisma3[0][a]++;
  113.             }
  114.     }
  115.     }
  116.     for(i=0;i<3;i++){
  117.         for(j=3;j<6;j++){
  118.             if(array[i][j]==a+1)
  119.             {
  120.                 athroisma3[1][a]++;
  121.             }
  122.         }
  123.     }
  124.     for(i=0;i<3;i++){
  125.         for(j=6;j<9;j++)
  126.         {
  127.             if(array[i][j]==a+1)
  128.             {
  129.                 athroisma3[2][a]++;
  130.             }
  131.         }
  132.     }
  133.     for(i=3;i<6;i++)
  134.     {
  135.         for(j=0;j<3;j++)
  136.         {
  137.             if(array[i][j]==a+1)
  138.             {
  139.                 athroisma3[3][a]++;
  140.             }
  141.         }
  142.     }
  143.     for(i=3;i<6;i++){
  144.         for(j=3;j<6;j++){
  145.             if(array[i][j]==a+1)
  146.             {
  147.                 athroisma3[4][a]++;
  148.             }
  149.         }
  150.     }
  151.     for(i=3;i<6;i++){
  152.         for(j=6;j<9;j++)
  153.         {
  154.             if(array[i][j]==a+1)
  155.             {
  156.                 athroisma3[5][a]++;
  157.             }
  158.         }
  159.     }
  160.     for(i=6;i<9;i++){
  161.         for(j=0;j<3;j++){
  162.             if(array[i][j]==a+1){
  163.                 athroisma3[6][a]++;
  164.             }
  165.         }
  166.     }
  167.     for(i=6;i<9;i++){
  168.         for(j=3;j<6;j++){
  169.             if(array[i][j]==a+1)
  170.             {
  171.                 athroisma3[7][a]++;
  172.             }
  173.         }
  174.     }
  175.     for(i=6;i<9;i++){
  176.         for(j=6;j<9;j++){
  177.             if(array[i][j]==a+1){
  178.                 athroisma3[8][a]++;
  179.             }
  180.         }
  181.     }
  182.     }
  183.     for(i=0;i<9;i++){
  184.         for(j=0;j<9;j++)
  185.         {
  186.                     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 */
  187.                         sudoku=0;
  188.             }
  189.         }
  190.     }
  191.     if (sudoku == 1)
  192.     {
  193.         printf("OK"); /* elegxei an to sudoku ontos einai sosto kai tiponi to katalilo minima */
  194.     } else {
  195.         printf(" NOT OK, THATS NOT A VALID SUDOKU \n");
  196.         for (i=0;i<9;i++){
  197.             for(j=0;j<9;j++)
  198.             {
  199.                         if(athroisma1[i][j]>1){ /*arrayn den einarrayi, vriskei se poies grarraymmes, se poies sthles karrayi se poiarray blocks entopiarrayetarrayi to provlhmarray*/
  200.                             checkcolumn[i]=1;
  201.                         }
  202.                         if(athroisma2[i][j]>1){
  203.                             checkrow[i]=1;
  204.                         }
  205.                         if(athroisma3[i][j]>1){
  206.                             checkblock[i]=1;
  207.                         }
  208.                         }
  209.  
  210.  
  211.     }
  212.  
  213.                 printf("Invalid rows: \n "); /*Tiponi se pies rows,columns kai blocks iparxi lathos sto sudoku*/
  214.  
  215.                 for (i=0;i<9;i++)
  216.                 {
  217.                 if(checkrow[i]==1)
  218.                 {
  219.                     printf("%d",i+1);
  220.                 }
  221.                 }
  222.                 printf("Invalid columns: \n  ");
  223.                 for (i=0;i<9;i++){
  224.                 if(checkcolumn[i]==1){
  225.                     printf(" %d",i+1);
  226.                 }
  227.                 }
  228.                 printf("Invalid blocks: \n");
  229.                 for (i=0;i<9;i++)
  230.                 {
  231.                 if(checkblock[i]==1)
  232.                 {
  233.                     printf("%d",i+1);
  234.                 }
  235.     }
  236.     }
  237.     printf("\n");
  238. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement