Code.GeekInterview.com
 
Code Samples C
 

Brute force solver for Challenger number puzzle.


Code ResourceAuthor: Charles Pergiel  

Difficulty Level: Beginner

Published: 1st Oct 2010   Read: 3507 times  

Filed in: C
Add Comment


 

 

Sponsored Links


 

 
On my old Dell PC, this program (with a bug in it) ran for five hours before it exhausted all possibilities and quit. After I fixed the bugs, it took 20 minutes to find the solution. It only took about an hour to write and debug. I just wanted to see how long it would take to find the answer.
 


Sample Code
  1. char*   title = "Program to solve Challenger game.";
  2.  
  3. #include        <stdio.h>
  4. #include        <string.h>
  5.  
  6. #define EOL             "\r\n"
  7. #define START   1
  8. #define STOP    9
  9. #define writeln(p)      printf("%s" EOL, p)
  10. #define zerofill(p)     memset(p, 0, sizeof(p))
  11.  
  12. typedef unsigned char   boolean;
  13.  
  14. int     row_totals      [4];
  15. int     column_totals   [4];
  16. int     up_diagonal_total;
  17. int     down_diagonal_total;
  18.  
  19. struct
  20. {
  21.         int     v;
  22.         boolean known;
  23. }       a[4][4];
  24.  
  25.  
  26. boolean valid(void)
  27. {
  28.         int     x;
  29.         int     y;
  30.         int     total;
  31.  
  32.         for (x=0; x<4; x++)
  33.         {
  34.         total = 0;
  35.         for (y=0; y<4; y++)
  36.                 total += a[x][y].v;
  37.         if (total != column_totals[x])
  38.                 return 0;
  39.         }
  40.  
  41.         for (y=0; y<4; y++)
  42.         {
  43.         total = 0;
  44.         for (x=0; x<4; x++)
  45.                 total += a[x][y].v;
  46.         if (total != row_totals[y])
  47.                 return 0;
  48.         }
  49.  
  50.         total = 0;
  51.         for (x=0; x<4; x++)
  52.         total += a[x][x].v;
  53.  
  54.         if (total != down_diagonal_total)
  55.         return 0;
  56.  
  57.         total = 0;
  58.         for (x=0; x<4; x++)
  59.         total += a[x][3-x].v;
  60.  
  61.         if (total != up_diagonal_total)
  62.         return 0;
  63.  
  64.         return 1;
  65. }
  66.  
  67.  
  68.  
  69. boolean increment(void)
  70. {
  71.         int     x;
  72.         int     y;
  73.  
  74.         for (x=0; x<4; x++)
  75.         {
  76.         for (y=0; y<4; y++)
  77.         {
  78.                 if (!a[x][y].known)
  79.                 {
  80.                 a[x][y].v++;
  81.                 if (a[x][y].v > STOP)
  82.                         a[x][y].v = START;
  83.                 else
  84.                         return 0;
  85.                 }
  86.         }
  87.         }
  88.         return 1;
  89. }
  90.  
  91.  
  92.  
  93. void init(void)
  94. {
  95.         zerofill(a              );
  96.         zerofill(row_totals     );
  97.         zerofill(column_totals  );
  98.         up_diagonal_total       = 0;
  99.         down_diagonal_total     = 0;
  100. }
  101.  
  102.  
  103.  
  104. void show_result(void)
  105. {
  106.         int     x;
  107.         int     y;
  108.  
  109.         printf("%d"     EOL, up_diagonal_total);
  110.         for (y=0; y<4; y++)
  111.         {
  112.         for (x=0; x<4; x++)
  113.                 printf("%d", a[x][y].v);
  114.  
  115.         printf("%d" EOL, row_totals[y]);
  116.         }
  117.         for (x=0; x<4; x++)
  118.         printf("%d", column_totals[x]);
  119.  
  120.         printf("%d"     EOL, down_diagonal_total);
  121. }
  122.  
  123.  
  124.  
  125. void main(void)
  126. {
  127.         int     x;
  128.         int     y;
  129.         int     count   =          1;
  130.         int     counter = 10000000;
  131.  
  132.         writeln(title);
  133.        
  134.         init();
  135.  
  136. // Set known values
  137. //                      32
  138. //      9               31
  139. //              6       28
  140. //      8               28
  141. //              7       33
  142. //      30      33      31      26      28
  143.  
  144.         a[1][0].v = 9;  a[1][0].known = 1;
  145.         a[2][1].v = 6;  a[2][1].known = 1;
  146.         a[0][2].v = 8;  a[0][2].known = 1;
  147.         a[3][3].v = 7;  a[3][3].known = 1;
  148.         row_totals      [0]     = 31;
  149.         row_totals      [1] = 28;
  150.         row_totals      [2] = 28;
  151.         row_totals      [3] = 33;
  152.         column_totals   [0] = 30;
  153.         column_totals   [1] = 33;
  154.         column_totals   [2] = 31;
  155.         column_totals   [3] = 26;
  156.         up_diagonal_total       = 32;
  157.         down_diagonal_total     = 28;
  158.  
  159.         for (x=0; x<4; x++)
  160.         {
  161.         for (y=0; y<4; y++)
  162.                 if (!a[x][y].known)
  163.                 a[x][y].v = START;
  164.         }
  165.  
  166.         while (1)
  167.         {
  168.         counter--;
  169.         if (counter==0)
  170.         {
  171.                 printf("count = %d" EOL, count++);
  172.                 counter = 10000000;
  173.         }
  174.         if (valid())
  175.         {
  176.                 show_result();
  177.                 return;
  178.         }
  179.         if (increment())
  180.                 break;
  181.         }
  182.         writeln("No solution");
  183. }
  184.  
Copyright GeekInterview.com


Next Article: C Programme to Print Without Semi colon


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. kaivalya198933891
2. mano.mithun21566
3. deepu0817849
4. meefriend4ever8112
5. chandrikakr8081
6. sadasivathavamani7316
7. shashivaishnav7137
8. venkatakrishnansvpr7039
9. Jimmy Zorald6684
10. sripri5701

Active Coders

Refined Tags