Advertisement
FractalFusion

Draws for 6x6 Hip Game

Jan 6th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | None | 0 0
  1. //This program finds draws for 6x6 Hip Game (http://delphiforfun.org/Programs/HIP.htm)
  2. //The base draw positions are:
  3. //0 1 0 0 0 1    0 1 0 0 0 1    0 1 0 0 0 1    0 1 0 1 0 1
  4. //1 1 1 1 0 0    1 1 1 1 0 0    1 1 1 1 0 0    1 1 0 0 0 0
  5. //0 0 1 0 0 1    1 0 1 0 0 1    1 0 1 0 0 1    0 1 1 0 1 1
  6. //1 0 0 1 0 1    1 0 0 1 0 0    1 0 0 1 0 1    1 1 0 1 1 0
  7. //0 0 1 1 1 1    0 0 1 1 1 1    0 0 1 1 1 1    0 0 0 0 1 1
  8. //1 0 1 0 1 0    1 0 1 0 1 0    1 0 0 0 1 0    1 0 1 0 1 0
  9.  
  10. #include <stdio.h>
  11. #define SIZE 6
  12. #define TRUE 1
  13.  
  14. int main()
  15. {
  16.     int h[16][16];
  17.    
  18.     for(int i=0;i<=15;i++) for(int j=0;j<=15;j++) h[i][j]=-1;
  19.    
  20.     h[0][0]=0;
  21.    
  22.    
  23.     int iter1=0;
  24.     int iter2=1;
  25.     int solutioncount=0;
  26.     while(TRUE)
  27.     {
  28.         if(iter1==0 && iter2==0) break;
  29.        
  30.         h[iter1][iter2]++;
  31.        
  32.         if(h[iter1][iter2]>=2)
  33.         {
  34.             //go back
  35.             h[iter1][iter2]=-1;
  36.             iter2--;
  37.             if(iter2<=-1)
  38.             {
  39.                 iter1--;
  40.                 iter2=SIZE-1;
  41.             }
  42.             continue;
  43.         }
  44.         else
  45.         {
  46.             //check if valid
  47.             //adjacent corner of square can be (a,b) satisfying:
  48.             //a<=iter1, b<iter2
  49.             //(iter2-b)+(iter1-a)<=iter1
  50.             //(iter1-a)<=SIZE-1-iter2
  51.            
  52.             int valid=1;
  53.             for(int a=0;a<=iter1;a++) for(int b=0;b<iter2;b++)
  54.             {
  55.                 if(iter2-b+iter1-a>iter1 || iter1-a>=SIZE-iter2) continue;
  56.                
  57.                 if(h[iter1][iter2]==h[a][b] && h[a][b]==h[a-(iter2-b)][b+(iter1-a)] && h[a][b]==h[iter1-(iter2-b)][iter2+(iter1-a)])
  58.                     valid=0;
  59.                
  60.             }
  61.            
  62.             if(!valid) continue;
  63.            
  64.             //go forward
  65.             iter2++;
  66.             if(iter2>=SIZE)
  67.                 if(iter1>=SIZE-1)
  68.                 {
  69.                     //solution
  70.                    
  71.                     //print those which have 18 1's
  72.                    
  73.                     int countones=0;
  74.                     for(int ii=0;ii<SIZE;ii++)  for(int jj=0;jj<SIZE;jj++)
  75.                     {
  76.                         if(h[ii][jj]==1) countones++;
  77.                     }
  78.                     if(countones==SIZE*SIZE/2)
  79.                     {
  80.                    
  81.                         for(int i=0;i<SIZE;i++)
  82.                         {
  83.                             for(int j=0;j<SIZE;j++) printf("%d ",h[i][j]);
  84.                             printf("\n");
  85.                         }
  86.                         printf("\n");
  87.                         iter2--;
  88.                         solutioncount++;
  89.                    
  90.                     }
  91.                     else
  92.                         iter2--;
  93.                    
  94.                 }
  95.                 else
  96.                 {
  97.                     iter1++;
  98.                     iter2=0;
  99.                 }
  100.            
  101.         }
  102.        
  103.     }
  104.    
  105.     printf("Number of solutions: %d", solutioncount);
  106.  
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement