0

Hi is there anyone willing enough to help me solve my problem. I'm using both Dev C++ 5 in windows and g++ in Linux to compile this code but was unable to do it successfully. Can any one help mi pliz

#include <cstdlib> 
#include <iostream> 
   using namespace std; 
// note: sky 1905 puzzle 
   int g[]={5,0,6,0,2,0,9,0,3, 
         0,0,8,0,0,0,5,0,0, 
         0,0,0,0,0,0,0,0,0, 
         6,0,0,2,8,5,0,0,9, 
         0,0,0,9,0,3,0,0,0, 
         8,0,0,7,6,1,0,0,4, 
         0,0,0,0,0,0,0,0,0, 
         0,0,4,0,0,0,3,0,0, 
         2,0,1,0,5,0,6,0,7}; 
 
   long m[108]; 
   long r[10]; 
   int p; 
   bool FirstSolution; 
   int Solutions; 
   long sg[81][81]; 
   int sp=0; 
    void rarray () 
   { 
   // Populate r[] table with values. 
      r[0]=0  ; r[1]=2  ; r[2]=4  ; r[3]=8  ; r[4]=16; 
      r[5]=32 ; r[6]=64 ; r[7]=128; r[8]=256; r[9]=512; 
   } 
    void PrintGrid() 
   { 
      printf("\n"); 
      int i=0; 
      while (i<81) 
      { 
         printf( "%d",g[i]); 
         if (((i+1)%9)==0) { printf("\n");  } 
         i++; 
      } 
      printf("\n"); 
   } 
    void or_horizontal() { 
   // Create Horizontal Masks by OR-ing 2^(grid values) together, 
   // via a lookup table for speed. 
      int p;                                            
      int q; 
      int i = 0; 
      while (i < 9) { 
         p = 81 + i; 
         q = 9 * i; 
         m[p] = r[g[q]] |  r[g[q+1]] |  r[g[q+2]] |  r[g[q+3]] |  r[g[q+4]] |  r[g[q+5]] |  r[g[q+6]] |  r[g[q+7]] |  r[g[q+8]]; 
         i++; 
      } 
   } 
    void or_verticals() { 
   // Create Vertical Masks by OR-ing 2^(grid values) together, 
   // via a lookup table for speed. 
      int p; 
      int q; 
      int i = 0; 
      while (i < 9) { 
         p =90 + i; 
         m[p] = r[g[i]] | r[g[9+i]] |  r[g[18+i]] |  r[g[27+i]] |  r[g[36+i]] |  r[g[45+i]] |  r[g[54+i]] |  r[g[63+i]] |  r[g[72+i]]; 
         i++; 
      } 
   } 
    void or_clusters() { 
   // Create Cluster Masks by OR-ing 2^(grid values) together, 
   // via a lookup table for speed. 
      m[ 99] = r[g[ 0]] | r[g[ 1]] | r[g[ 2]] | r[g[ 9]] | r[g[10]] | r[g[11]] | r[g[18]] | r[g[19]] | r[g[20]] ; 
      m[100] = r[g[ 3]] | r[g[ 4]] | r[g[ 5]] | r[g[12]] | r[g[13]] | r[g[14]] | r[g[21]] | r[g[22]] | r[g[23]] ; 
      m[101] = r[g[ 6]] | r[g[ 7]] | r[g[ 8]] | r[g[15]] | r[g[16]] | r[g[17]] | r[g[24]] | r[g[25]] | r[g[26]] ; 
      m[102] = r[g[27]] | r[g[28]] | r[g[29]] | r[g[36]] | r[g[37]] | r[g[38]] | r[g[45]] | r[g[46]] | r[g[47]] ; 
      m[103] = r[g[30]] | r[g[31]] | r[g[32]] | r[g[39]] | r[g[40]] | r[g[41]] | r[g[48]] | r[g[49]] | r[g[50]] ; 
      m[104] = r[g[33]] | r[g[34]] | r[g[35]] | r[g[42]] | r[g[43]] | r[g[44]] | r[g[51]] | r[g[52]] | r[g[53]] ; 
      m[105] = r[g[54]] | r[g[55]] | r[g[56]] | r[g[63]] | r[g[64]] | r[g[65]] | r[g[72]] | r[g[73]] | r[g[75]] ; 
      m[106] = r[g[57]] | r[g[58]] | r[g[59]] | r[g[66]] | r[g[67]] | r[g[68]] | r[g[75]] | r[g[76]] | r[g[78]] ; 
      m[107] = r[g[60]] | r[g[61]] | r[g[62]] | r[g[69]] | r[g[70]] | r[g[71]] | r[g[78]] | r[g[79]] | r[g[80]] ; 
 
   } 
 
    void or_squares() { 
   // Create individual masks by OR-ing Horizontal Mask, Vertical Mask and Cluser Mask. 
      m[ 0]=m[81] | m[90] | m[ 99]; m[ 1]=m[81] | m[91] | m[ 99]; m[ 2]=m[81] | m[92] | m[ 99]; 
      m[ 3]=m[81] | m[93] | m[100]; m[ 4]=m[81] | m[94] | m[100]; m[ 5]=m[81] | m[95] | m[100]; 
      m[ 6]=m[81] | m[96] | m[101]; m[ 7]=m[81] | m[97] | m[101]; m[ 8]=m[81] | m[98] | m[101]; 
      m[ 9]=m[82] | m[90] | m[ 99]; m[10]=m[82] | m[91] | m[ 99]; m[11]=m[82] | m[92] | m[ 99]; 
      m[12]=m[82] | m[93] | m[100]; m[13]=m[82] | m[94] | m[100]; m[14]=m[82] | m[95] | m[100]; 
      m[15]=m[82] | m[96] | m[101]; m[16]=m[82] | m[97] | m[101]; m[17]=m[82] | m[98] | m[101]; 
      m[18]=m[83] | m[90] | m[ 99]; m[19]=m[83] | m[91] | m[ 99]; m[20]=m[83] | m[92] | m[ 99]; 
      m[21]=m[83] | m[93] | m[100]; m[22]=m[83] | m[94] | m[100]; m[23]=m[83] | m[95] | m[100]; 
      m[24]=m[83] | m[96] | m[101]; m[25]=m[83] | m[97] | m[101]; m[26]=m[83] | m[98] | m[101]; 
      m[27]=m[84] | m[90] | m[102]; m[28]=m[84] | m[91] | m[102]; m[29]=m[84] | m[92] | m[102]; 
      m[30]=m[84] | m[93] | m[103]; m[31]=m[84] | m[94] | m[103]; m[32]=m[84] | m[95] | m[103]; 
      m[33]=m[84] | m[96] | m[104]; m[34]=m[84] | m[97] | m[104]; m[35]=m[84] | m[98] | m[104]; 
      m[36]=m[85] | m[90] | m[102]; m[37]=m[85] | m[91] | m[102]; m[38]=m[85] | m[92] | m[102]; 
      m[39]=m[85] | m[93] | m[103]; m[40]=m[85] | m[94] | m[103]; m[41]=m[85] | m[95] | m[103]; 
      m[42]=m[85] | m[96] | m[104]; m[43]=m[85] | m[97] | m[104]; m[44]=m[85] | m[98] | m[104]; 
      m[45]=m[86] | m[90] | m[102]; m[46]=m[86] | m[91] | m[102]; m[47]=m[86] | m[92] | m[102]; 
      m[48]=m[86] | m[93] | m[103]; m[49]=m[86] | m[94] | m[103]; m[50]=m[86] | m[95] | m[103]; 
      m[51]=m[86] | m[96] | m[104]; m[52]=m[86] | m[97] | m[104]; m[53]=m[86] | m[98] | m[104]; 
      m[54]=m[87] | m[90] | m[105]; m[55]=m[87] | m[91] | m[105]; m[56]=m[87] | m[92] | m[105]; 
      m[57]=m[87] | m[93] | m[106]; m[58]=m[87] | m[94] | m[106]; m[59]=m[87] | m[95] | m[106]; 
      m[60]=m[87] | m[96] | m[107]; m[61]=m[87] | m[97] | m[107]; m[62]=m[87] | m[98] | m[107]; 
      m[63]=m[88] | m[90] | m[105]; m[64]=m[88] | m[91] | m[105]; m[65]=m[88] | m[92] | m[105]; 
      m[66]=m[88] | m[93] | m[106]; m[67]=m[88] | m[94] | m[106]; m[68]=m[88] | m[95] | m[106]; 
      m[69]=m[88] | m[96] | m[107]; m[70]=m[88] | m[97] | m[107]; m[71]=m[88] | m[98] | m[107]; 
      m[72]=m[89] | m[90] | m[105]; m[73]=m[89] | m[91] | m[105]; m[74]=m[89] | m[92] | m[105]; 
      m[75]=m[89] | m[93] | m[106]; m[76]=m[89] | m[94] | m[106]; m[77]=m[89] | m[95] | m[106]; 
      m[78]=m[89] | m[96] | m[107]; m[79]=m[89] | m[97] | m[107]; m[80]=m[89] | m[98] | m[107]; 
   } 
 
    void or_masks() { 
   // Perform masks 
      or_horizontal(); 
      or_verticals(); 
      or_clusters(); 
      or_squares(); 
   } 
    bool not_impossible() { 
   // Check grid to see if it is impossible. 
   // If a square is empty but has a mask of 1022, then the grid is impossible. 
      int i=0; 
      bool s=true; 
      while ((i<81) && (s=true)) { 
         if ((m[i]==1022) && (g[i]==0)) { 
            s=false;  
         } 
         i++; 
      } 
      return s; 
   } 
    bool fullgrid() { 
      int i=0; 
      bool s=true; 
      while ((i<81) && (s==true)) 
      { 
         if (g[i]==0)  { s=false ;  } 
         i++; 
      }    
      return s; 
   } 
    void findsingles() { 
   // Find Squares that can only be one value. 
      int i=0; 
      while (i<81) 
      { 
         if (g[i]==0) // Is square empty? 
         { 
            switch (m[i])  //What's it mask? 
            { 
               case  510: g[i]=9; 
                  break; 
               case  766: g[i]=8; 
                  break; 
               case  894: g[i]=7; 
                  break; 
               case  958: g[i]=6; 
                  break; 
               case  990: g[i]=5; 
                  break; 
               case 1006: g[i]=4; 
                  break; 
               case 1014: g[i]=3; 
                  break; 
               case 1018: g[i]=2; 
                  break; 
               case 1020: g[i]=1; 
                  break; 
               default: 
                  break; 
            }    
            or_masks(); 
         } 
         i++; 
      } 
   } 
 
    void stack() 
   { 
   //printf("stack "); 
      sp++; 
      sg[sp][ 0]=g[ 0]; sg[sp][ 1]=g[ 1]; sg[sp][ 2]=g[ 2]; sg[sp][ 3]=g[ 3]; sg[sp][ 4]=g[ 4]; sg[sp][ 5]=g[ 5]; 
      sg[sp][ 6]=g[ 6]; sg[sp][ 7]=g[ 7]; sg[sp][ 8]=g[ 8]; sg[sp][ 9]=g[ 9]; sg[sp][10]=g[10]; sg[sp][11]=g[11]; 
      sg[sp][12]=g[12]; sg[sp][13]=g[13]; sg[sp][14]=g[14]; sg[sp][15]=g[15]; sg[sp][16]=g[16]; sg[sp][17]=g[17]; 
      sg[sp][18]=g[18]; sg[sp][19]=g[19]; sg[sp][20]=g[20]; sg[sp][21]=g[21]; sg[sp][22]=g[22]; sg[sp][23]=g[23]; 
      sg[sp][24]=g[24]; sg[sp][25]=g[25]; sg[sp][26]=g[26]; sg[sp][27]=g[27]; sg[sp][28]=g[28]; sg[sp][29]=g[29]; 
      sg[sp][30]=g[30]; sg[sp][31]=g[31]; sg[sp][32]=g[32]; sg[sp][33]=g[33]; sg[sp][34]=g[34]; sg[sp][35]=g[35]; 
      sg[sp][36]=g[36]; sg[sp][37]=g[37]; sg[sp][38]=g[38]; sg[sp][39]=g[39]; sg[sp][40]=g[40]; sg[sp][41]=g[41]; 
      sg[sp][42]=g[42]; sg[sp][43]=g[43]; sg[sp][44]=g[44]; sg[sp][45]=g[45]; sg[sp][46]=g[46]; sg[sp][47]=g[47]; 
      sg[sp][48]=g[48]; sg[sp][49]=g[49]; sg[sp][50]=g[50]; sg[sp][51]=g[51]; sg[sp][52]=g[52]; sg[sp][53]=g[53]; 
      sg[sp][54]=g[54]; sg[sp][55]=g[55]; sg[sp][56]=g[56]; sg[sp][57]=g[57]; sg[sp][58]=g[58]; sg[sp][59]=g[59]; 
      sg[sp][60]=g[60]; sg[sp][61]=g[61]; sg[sp][62]=g[62]; sg[sp][63]=g[63]; sg[sp][64]=g[64]; sg[sp][65]=g[65]; 
      sg[sp][66]=g[66]; sg[sp][67]=g[67]; sg[sp][68]=g[68]; sg[sp][69]=g[69]; sg[sp][70]=g[70]; sg[sp][71]=g[71]; 
      sg[sp][72]=g[72]; sg[sp][73]=g[73]; sg[sp][74]=g[74]; sg[sp][75]=g[75]; sg[sp][76]=g[76]; sg[sp][77]=g[77]; 
      sg[sp][78]=g[78]; sg[sp][79]=g[79]; sg[sp][80]=g[80];          
   } 
    void unstack() 
   { 
      g[ 0]=sg[sp][ 0]; g[ 1]=sg[sp][ 1]; g[ 2]=sg[sp][ 2]; g[ 3]=sg[sp][ 3]; g[ 4]=sg[sp][ 4]; g[ 5]=sg[sp][ 5]; 
      g[ 6]=sg[sp][ 6]; g[ 7]=sg[sp][ 7]; g[ 8]=sg[sp][ 8]; g[ 9]=sg[sp][ 9]; g[10]=sg[sp][10]; g[11]=sg[sp][11]; 
      g[12]=sg[sp][12]; g[13]=sg[sp][13]; g[14]=sg[sp][14]; g[15]=sg[sp][15]; g[16]=sg[sp][16]; g[17]=sg[sp][17]; 
      g[18]=sg[sp][18]; g[19]=sg[sp][19]; g[20]=sg[sp][20]; g[21]=sg[sp][21]; g[22]=sg[sp][22]; g[23]=sg[sp][23]; 
      g[24]=sg[sp][24]; g[25]=sg[sp][25]; g[26]=sg[sp][26]; g[27]=sg[sp][27]; g[28]=sg[sp][28]; g[29]=sg[sp][29]; 
      g[30]=sg[sp][30]; g[31]=sg[sp][31]; g[32]=sg[sp][32]; g[33]=sg[sp][33]; g[34]=sg[sp][34]; g[35]=sg[sp][35]; 
      g[36]=sg[sp][36]; g[37]=sg[sp][37]; g[38]=sg[sp][38]; g[39]=sg[sp][39]; g[40]=sg[sp][40]; g[41]=sg[sp][41]; 
      g[42]=sg[sp][42]; g[43]=sg[sp][43]; g[44]=sg[sp][44]; g[45]=sg[sp][45]; g[46]=sg[sp][46]; g[47]=sg[sp][47]; 
      g[48]=sg[sp][48]; g[49]=sg[sp][49]; g[50]=sg[sp][50]; g[51]=sg[sp][51]; g[52]=sg[sp][52]; g[53]=sg[sp][53]; 
      g[54]=sg[sp][54]; g[55]=sg[sp][55]; g[56]=sg[sp][56]; g[57]=sg[sp][57]; g[58]=sg[sp][58]; g[59]=sg[sp][59]; 
      g[60]=sg[sp][60]; g[61]=sg[sp][61]; g[62]=sg[sp][62]; g[63]=sg[sp][63]; g[64]=sg[sp][64]; g[65]=sg[sp][65]; 
      g[66]=sg[sp][66]; g[67]=sg[sp][67]; g[68]=sg[sp][68]; g[69]=sg[sp][69]; g[70]=sg[sp][70]; g[71]=sg[sp][71]; 
      g[72]=sg[sp][72]; g[73]=sg[sp][73]; g[74]=sg[sp][74]; g[75]=sg[sp][75]; g[76]=sg[sp][76]; g[77]=sg[sp][77]; 
      g[78]=sg[sp][78]; g[79]=sg[sp][79]; g[80]=sg[sp][80]; 
 
      sp--; 
   } 
 
    void AddToSolutions() 
   { 
 
      Solutions++; 
      printf("\n%d ={",Solutions); 
      PrintGrid(); 
      printf("}\n"); 
   } 
    bool validSelection(int value,int pos) 
   { 
   //printf("%d",(m[gv] & r[gp]) == 0); 
      return( (m[pos] & r[value]) == 0); 
   } 
 
 
 
void SolveThis() 
{ 
// if ((FirstSolution=true) && (Solutions>0))  { return; } 
 if (not_impossible()==false) { return; } 
 if (p<=80) { 
    or_masks(); 
  findsingles(); 
 
  while ((g[p] != 0) && (p < 81)) { ++p; } 
  if (g[p]==0) { 
     printf("%d ",p); 
   for (int v=1;v<10;v++) { 
    if (validSelection(v,p)==true) { 
     stack(); 
     g[p]=v; 
     or_masks(); 
     findsingles(); 
      ++p; 
      SolveThis(); 
      --p; 
      unstack(); 
      or_masks(); 
     } 
    } 
  } 
 } 
 else 
 { 
  if (fullgrid()==true) { 
  AddToSolutions();     } 
 } 
} 
 
 
 
    int main(int argc, char *argv[]) 
   { 
      rarray(); 
      printf("Sudoko Solver\n"); 
   // printf("= %d",g[0]); 
      PrintGrid(); 
      p=0; 
      FirstSolution=false; 
   //Solutions=0; 
 
      or_masks(); 
      SolveThis(); 
      system("PAUSE"); 
      return EXIT_SUCCESS; 
   }
3
Contributors
3
Replies
4
Views
11 Years
Discussion Span
Last Post by risby
0

What were the compile errors? Give the error messages and the lines where the error occured. Don't give the line numbers. We can't count.

0

What were the compile errors? Give the error messages and the lines where the error occured. Don't give the line numbers. We can't count.

I got the problem figured out. I forgot to include the <stdio.h> library. The problem was an implicit declaration of the function printf(....).
Thanks alot for the immediate response. The program is now running.

1

Hi is there anyone willing enough to help me solve my problem. I'm using both Dev C++ 5 in windows and g++ in Linux to compile this code but was unable to do it successfully. Can any one help mi pliz

bool not_impossible() { 
   // Check grid to see if it is impossible. 
   // If a square is empty but has a mask of 1022, then the grid is impossible. 
      int i=0; 
      bool s=true; 
      while ((i<81) && ([B]s=true[/B])) { 
         if ((m[i]==1022) && (g[i]==0)) { 
            s=false;  
         } 
         i++; 
      } 
      return s; 
   }

I presume this should be a test for equality rather than an assignment.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.