Advertisement
image28

Solves some Sudoku Puzzles - INCOMPLETE

May 6th, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.83 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. typedef struct tables
  4. {
  5.     int bit[9][9];
  6. };
  7.  
  8. int main(int argc, char *argv[])
  9. {
  10.     if ( argc < 81 )
  11.     {
  12.         printf("Incorrect number of arguments\n");
  13.         exit(-1);
  14.     }
  15.  
  16.     int x,y,d,e,l;
  17.     int count=0;
  18.     int temp=0;
  19.     int prev=0;
  20.     int combo=0;
  21.     struct tables bits[9];
  22.     int intable[9][9];
  23.  
  24.     // Initialize All Tables
  25.     zero(&intable);
  26.     for(d=0;d<9;d++)
  27.     {
  28.         zero(&bits[d].bit);
  29.     }
  30.  
  31.     // Fill intable Table With Arguments
  32.     count=1;
  33.  
  34.     for(x=0;x<9;x++)
  35.     {
  36.         for(y=0;y<9;y++)
  37.         {
  38.             temp=atoi(argv[count]);
  39.             intable[x][y]=temp;
  40.             count++;
  41.         }
  42.     }
  43.  
  44.     // Print intable Table
  45.     printf("input Table\n-----------------\n");
  46.     print(&intable);
  47.  
  48.     // Fill bit tables
  49.     while ( ( done(&intable) ) && ( done(&intable) != prev ) )
  50.     {
  51.         prev=done(&intable);
  52.  
  53.         /*for(d=0;d<9;d++)
  54.         {
  55.             zero(&bits[d].bit);
  56.         }*/
  57.         fillbits(&intable,&bits);
  58.  
  59.         fillnums(&intable,&bits);
  60.     }
  61.  
  62.     // Print bit tables ( DEBUG )
  63.     for(d=0;d<9;d++)
  64.     {
  65.         printf("Table %d\n-----------------\n",d+1);
  66.         print(&bits[d].bit);
  67.     }
  68.  
  69.     printf("Solved\n---------------\n");
  70.     print(&intable);
  71.  
  72.     return(0);
  73. }
  74.  
  75. int done(int *table)
  76. {
  77.     int x,y;
  78.     int done=0;
  79.  
  80.     for(x=0;x<9;x++)
  81.     {
  82.         for(y=0;y<9;y++)
  83.         {
  84.             if ( *(table+((y*9)+x)) == 0 )
  85.             {
  86.                 done++;
  87.             }
  88.         }
  89.     }
  90.  
  91.     return(done);
  92. }
  93.  
  94. int fillnums(int *table, struct tables *box)
  95. {
  96.     int x,y,d,e;
  97.     int count=1;
  98.     int posx=0;
  99.     int posy=0;
  100.     int zeros=0;
  101.  
  102.     while ( count < 10 )
  103.     {
  104.  
  105.         for(d=0;d<9;d=d+3)
  106.         {
  107.             for(e=0;e<9;e=e+3)
  108.             {
  109.                 for(x=0;x<3;x++)
  110.                 {
  111.                     for(y=0;y<3;y++)
  112.                     {
  113.                         if ( box[count-1].bit[e+y][d+x] == 0 )
  114.                         {
  115.                             zeros++;
  116.                             posx=d+x;
  117.                             posy=e+y;
  118.                         }
  119.                     }
  120.                 }
  121.  
  122.                 if ( zeros == 1 )
  123.                 {
  124.                     *(table+((posy*9)+posx))=count;
  125.                 }
  126.  
  127.                 zeros=0;
  128.  
  129.             }
  130.         }
  131.         count++;
  132.     }
  133.  
  134. }
  135.  
  136. int fillbits(int *table, struct tables *box)
  137. {
  138.     int x,y,d,e;
  139.     int startx=0;
  140.     int starty=0;
  141.     int count=1;
  142.     int zeros=0;
  143.  
  144.     while ( count < 10 )
  145.     {
  146.         for ( x=0; x < 9 ; x++ )
  147.         {
  148.  
  149.             for ( y=0; y < 9; y++ )
  150.             {
  151.  
  152.                 // set current table to 1 or 0 if the number is > 0
  153.                 if ( *(table+((y*9)+x)) == count )
  154.                 {
  155.                     if ( x < 3 )
  156.                     {
  157.                         startx=0;
  158.                     }else if ( x < 6 ){
  159.                         startx=3;
  160.                     }else if ( x < 9 ){
  161.                         startx=6;
  162.                     }
  163.  
  164.                     if ( y < 3 )
  165.                     {
  166.                         starty=0;
  167.                     }else if ( y < 6 ){
  168.                         starty=3;
  169.                     }else if ( y < 9 ){
  170.                         starty=6;
  171.                     }
  172.  
  173.                     for(d=0;d<9;d++)
  174.                     {
  175.                         box[d].bit[y][x]=1;
  176.                     }
  177.  
  178.                     for(d=startx;d<startx+3;d++)
  179.                     {
  180.                         for(e=starty;e<starty+3;e++)
  181.                         {
  182.                             box[count-1].bit[e][d]=1;
  183.                         }
  184.                     }
  185.  
  186.  
  187.                     for(d=0;d<9;d++)
  188.                     {
  189.                         box[count-1].bit[y][d]=1;
  190.                     }
  191.  
  192.                     for(e=0;e<9;e++)
  193.                     {
  194.                         box[count-1].bit[e][x]=1;
  195.                     }
  196.  
  197.                 }
  198.             }
  199.         }
  200.  
  201.         count++;
  202.     }count=1;
  203.  
  204.     // Find numbers with two places in a column or row, fill rest with ones
  205.     while ( count < 10 )
  206.     {
  207.         for(x=0;x<9;x=x+3)
  208.         {
  209.             for(y=0;y<9;y=y+3)
  210.             {
  211.                 for(d=0;d<3;d++)
  212.                 {
  213.                     for(e=0;e<3;e++)
  214.                     {
  215.                         if ( box[count-1].bit[y+e][x+d] == 0 )
  216.                         {
  217.                             zeros++;
  218.                         }
  219.                     }
  220.  
  221.                 }
  222.  
  223.                 if ( zeros == 2 )
  224.                 {
  225.  
  226.  
  227.                     zeros=0;
  228.                     for(d=0;d<3;d++)
  229.                     {
  230.                         for(e=0;e<3;e++)
  231.                         {
  232.                             if ( box[count-1].bit[y+e][x+d] == 0 )
  233.                             {
  234.                                 zeros++;
  235.                             }
  236.                         }
  237.  
  238.                         // fill vertical box
  239.                         if ( zeros == 2 )
  240.                         {
  241.                             for(e=0;e<9;e++)
  242.                             {
  243.                                 if ( ( e < y ) || ( e > y+2 ) )
  244.                                 {
  245.  
  246.                                     box[count-1].bit[e][x+d]=1;
  247.                                 }
  248.                             }
  249.                         }
  250.  
  251.                         zeros=0;
  252.                     }
  253.  
  254.                     zeros=0;
  255.                     for(d=0;d<3;d++)
  256.                     {
  257.                         for(e=0;e<3;e++)
  258.                         {
  259.                             if ( box[count-1].bit[y+d][x+e] == 0 )
  260.                             {
  261.                                 zeros++;
  262.                             }
  263.                         }
  264.  
  265.                         // fill horizontaly
  266.                         if ( zeros == 2 )
  267.                         {
  268.                             if ( count == 6 )
  269.                                 printf("%d %d %d %d\n",x,y,d,e);
  270.                             for(e=0;e<9;e++)
  271.                             {
  272.                                 if ( ( e < x ) || ( e > x+2 ) )
  273.                                 {
  274.  
  275.                                     box[count-1].bit[y+d][e]=1;
  276.                                 }
  277.                             }
  278.                         }
  279.  
  280.                         zeros=0;
  281.                     }
  282.                 }
  283.  
  284.                 zeros=0;
  285.             }
  286.         }
  287.         count++;
  288.     }
  289. }
  290.  
  291. int argsin(char *argv, int *table)
  292. {
  293.     int x,y;
  294.     int count=0;
  295.  
  296.     for(y=0;y<9;y++)
  297.     {
  298.         for(x=0;x<9;x++)
  299.         {
  300.             if ( atoi(*(argv+count)) != 0 )
  301.             {
  302.                 *(table+((y*9)+x))=atoi(*(argv+count));
  303.             }
  304.             count++;
  305.         }
  306.     }
  307.  
  308. }
  309.  
  310. int zero(int *table)
  311. {
  312.     int x,y;
  313.  
  314.     for(y=0;y<9;y++)
  315.     {
  316.         for(x=0;x<9;x++)
  317.         {
  318.             *(table+((y*9)+x))=0;
  319.             //printf("%d",*(table+((y*10)+x)));
  320.         }//printf("\n");
  321.     }
  322. }
  323.  
  324. int print(int *table)
  325. {
  326.     int x,y;
  327.     int space=0;
  328.     int space2=0;
  329.  
  330.     for(y=0;y<9;y++)
  331.     {
  332.         for(x=0;x<9;x++)
  333.         {
  334.             printf("%d", *(table+((y*9)+x)));
  335.             space++;
  336.  
  337.             if ( space == 3 )
  338.             {
  339.                 printf(" ");
  340.                 space=0;
  341.             }
  342.         }printf("\n");
  343.         space2++;
  344.  
  345.         if ( space2 == 3 )
  346.         {
  347.             printf("\n");
  348.             space2=0;
  349.         }
  350.     }
  351. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement