Advertisement
Guest User

Untitled

a guest
Nov 17th, 2011
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 KB | None | 0 0
  1. #include <set>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. /* Global Constants */
  7. // gNUMBER_OF_GAMES cannot be bigger than 6
  8. const int gNUMBER_OF_GAMES = 5;
  9. const int gGRID_SIZE = gNUMBER_OF_GAMES * gNUMBER_OF_GAMES;
  10. const int gNUMBER_OF_PLAYERS = 2*gNUMBER_OF_GAMES;
  11. const int gNUMBER_OF_PRIME_PRODUCTS = (gNUMBER_OF_PLAYERS * (gNUMBER_OF_PLAYERS - 1)) / 2;
  12.  
  13. /* Function prototypes */
  14. void decompose_prime(int num, int primeList[]);
  15. bool check_row(int myRow[]);
  16. bool check_grid(int myGrid[]);
  17. void print_grid(int myGrid[]);
  18. bool fill_grid(int myGrid[],int prodPrimes[]);
  19.  
  20. int main()
  21. {
  22.     int grid[gGRID_SIZE] = {0};
  23.    
  24.     const int myPrimes[] = {2,3,5,7,11,13,17,19,23,29,31,37};
  25.  
  26.     // Create an array of all products of primes.
  27.     int prodPrimes[gNUMBER_OF_PRIME_PRODUCTS];
  28.     int ctr = 0;
  29.     for (int i=0;i<gNUMBER_OF_PLAYERS-1;i++)
  30.     {
  31.         for (int j=i+1;j<gNUMBER_OF_PLAYERS;j++)
  32.         {
  33.             // Fill in the 'ctr' slot with primes[i]*primes[j]
  34.             prodPrimes[ctr++] = myPrimes[i]*myPrimes[j];
  35.         }
  36.     }
  37.    
  38.     if (fill_grid(grid,prodPrimes))
  39.         print_grid(grid);
  40.     else
  41.         printf("No solution found!\n");
  42.    
  43.     return 0;
  44. }
  45.  
  46. /* Sub-function definitions */
  47.  
  48. void decompose_prime(int num, int primeList[])
  49. {
  50.     /*
  51.     Decompose an int into primes
  52.     This only works in our special case where
  53.     num is the prod of two primes, each <= 37.
  54.     */
  55.    
  56.     static const int myPrimes[] = {2,3,5,7,11,13,17,19,23,29,31,37};
  57.    
  58.     if (num == 0)
  59.     {
  60.         primeList[0] = 0;
  61.         primeList[1] = 0;
  62.         return;
  63.     }
  64.     for (int i=0;i<gNUMBER_OF_PLAYERS;i++)
  65.     {
  66.         if (num % myPrimes[i] == 0)
  67.         {
  68.             primeList[0] = myPrimes[i];
  69.             primeList[1] = num/myPrimes[i];
  70.             return;
  71.         }
  72.     }
  73. }
  74.  
  75. bool check_row(int myRow[])
  76. {
  77.     /*
  78.     Check a row of a grid -- specifically, gNUMBER_OF_GAMES values
  79.     We need to make sure no prime factor is shared.
  80.     */
  81.     static int twoPrimes[2] = {0,0};
  82.     set<int> usedPrimes;
  83.     for (int k=0;k<gNUMBER_OF_GAMES;k++)
  84.     {
  85.         if (myRow[k] == 0)
  86.             continue;
  87.         decompose_prime(myRow[k], twoPrimes);
  88.         // See if the primes are in `usedPrimes`
  89.         for (int j=0;j<2;j++)
  90.         {
  91.             if (usedPrimes.find(twoPrimes[j]) != usedPrimes.end())
  92.                 return false;
  93.             else
  94.                 usedPrimes.insert(twoPrimes[j]);
  95.         }
  96.     }
  97.     return true;
  98. }
  99.  
  100. bool check_grid(int myGrid[])
  101. {
  102.     /* Check to make sure a given grid is okay (so far) */
  103.    
  104.     // First check: no two elements of the grid should be the same.
  105.     set<int> gridElements;
  106.     for (int i=0;i<gGRID_SIZE;i++)
  107.     {
  108.         if (myGrid[i] == 0)
  109.             continue;
  110.         if (gridElements.find(myGrid[i]) != gridElements.end())
  111.             return false;
  112.         else
  113.             gridElements.insert(myGrid[i]);
  114.     }
  115.    
  116.     // Go through each row
  117.     for (int i=0;i<gNUMBER_OF_GAMES;i++)
  118.     {
  119.         int* p_row = &myGrid[gNUMBER_OF_GAMES*i];
  120.         if (!check_row(p_row))
  121.             return false;
  122.     }
  123.    
  124.     // Go through each column
  125.     int col[gNUMBER_OF_GAMES] = {0};
  126.     for (int j=0;j<gNUMBER_OF_GAMES;j++)
  127.     {
  128.         for (int i=0;i<gNUMBER_OF_GAMES;i++)
  129.             col[i] = myGrid[gNUMBER_OF_GAMES*i+j];
  130.         if (!check_row(col))
  131.             return false;
  132.     }
  133.     return true;
  134. }  
  135.  
  136. void print_grid(int myGrid[])
  137. {
  138.     static int primeList[2] = {0,0};
  139.     for (int i=0;i<gNUMBER_OF_GAMES;i++)
  140.     {
  141.         for (int j=0;j<gNUMBER_OF_GAMES;j++)
  142.         {
  143.             decompose_prime(myGrid[gNUMBER_OF_GAMES*i+j], primeList);
  144.             printf("[%2i,%2i] ",primeList[0],primeList[1]);
  145.         }
  146.         printf("\n");
  147.     }
  148.     printf("-----\n");
  149. }
  150.  
  151. bool fill_grid(int myGrid[],int prodPrimes[])
  152. {
  153.     int gridCtr,prodCtr = 0;
  154.     while (gridCtr < gGRID_SIZE && gridCtr >= 0)
  155.     {
  156.         while (prodCtr < gNUMBER_OF_PRIME_PRODUCTS)
  157.         {
  158.             myGrid[gridCtr] = prodPrimes[prodCtr];
  159.             if (check_grid(myGrid))
  160.             {
  161.                 // Restart the process with greater gridCtr & prodCtr = 0
  162.                 gridCtr++;
  163.                 prodCtr = 0;
  164.                 break; // break out of the inner while loop
  165.             }
  166.             else
  167.                 prodCtr++;
  168.         }
  169.         /*
  170.         If we can't find an acceptable match above, we have to delete the
  171.         last entry and try again from the step before.
  172.         Find the previous prodCtr from myGrid[gridCtr-1]
  173.         */
  174.        
  175.         // Check that the above while loop gave nothing
  176.         if (prodCtr == gNUMBER_OF_PRIME_PRODUCTS)
  177.         {
  178.             // We need to set myGrid[gridCtr] and myGrid[gridCtr-1] to 0;
  179.             myGrid[gridCtr--] = 0;
  180.             for (int i=0;i<gNUMBER_OF_PRIME_PRODUCTS;i++)
  181.             {
  182.                 if (myGrid[gridCtr] == prodPrimes[i])
  183.                 {
  184.                     prodCtr = i+1;
  185.                     myGrid[gridCtr] = 0;
  186.                     break; // break out of the for loop
  187.                 }
  188.             }  
  189.         }
  190.     }
  191.     if (gridCtr == gGRID_SIZE)
  192.         return true;
  193.     return false;
  194. }  
  195.        
  196.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement