Recent Posts
XML | 0 sec ago
None | 3 sec ago
None | 6 sec ago
None | 6 sec ago
None | 9 sec ago
C++ | 14 sec ago
None | 15 sec ago
C++ | 20 sec ago
None | 31 sec ago
Lua | 39 sec ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By alexbobp on the 12th of Nov 2008 08:25:00 AM Download | Raw | Embed | Report
  1. /*
  2. -----BEGIN PGP SIGNED MESSAGE-----
  3. Hash: SHA1
  4.  
  5. */
  6.  
  7. /*
  8.  * Memoized Egg Dropper
  9.  * By Alex Ponebshek
  10.  *
  11.  * This is a program to solve the Google egg-dropping puzzle, generalized to more than 2 eggs.
  12.  * It uses memoization for efficiency.
  13.  * I still think it could be made more efficient by using a binary search to find the optimal drop floor.
  14.  *
  15.  * It can print the full decision tree for egg dropping.
  16.  */
  17.  
  18. public class EggDropper
  19. {
  20.         public static void main(String[] args)
  21.         {
  22.                 System.out.println("Dropping eggs...");
  23.                 int eggs=3;
  24.                 int stories=100;
  25.                
  26.                 EggDropper e=new EggDropper(eggs, stories); // Pepare the computat0r!
  27.                 System.out.println("It will take "+e.eggdrop(eggs, stories)+" drops."); // Print worst case drops
  28.                 e.eggdrop_treewalk(eggs, stories, 0, 0, false); // Print decision tree
  29.         }
  30.        
  31.         //f(1,b)=b, f(a,b)=1+max(f(a-1,x), f(a, b-x-1)) where x is whatever value of x would make f(a,b) the lowest
  32.        
  33.         private int[][] droppages;
  34.        
  35.         public EggDropper(int maxeggs, int maxlevels)
  36.         {
  37.                 droppages=new int[maxeggs-1][maxlevels-1];
  38.         }
  39.        
  40.         public int eggdrop(int a, int b)
  41.         {
  42.                 if (b==0) // No ambiguity, so no testing required
  43.                         return 0;
  44.                
  45.                 if (b==1) // One last drop!  I'm such an idiot for missing this before.
  46.                         return 1;
  47.                
  48.                 if (a<=1) // One egg, so exhaustive search
  49.                         return b;
  50.                
  51.                 if (droppages[a-2][b-2] == 0) // If it's not in the table...
  52.                         droppages[a-2][b-2]=eggdrop_calc(a, b); // calculate it!
  53.                
  54.                 return droppages[a-2][b-2];
  55.         }
  56.        
  57.         private int eggdrop_calc(int a, int b)
  58.         {
  59.                 for (int i=2; i<b; i++) // Precompute for lower floors to shake the obscenely deep recursion out ;)
  60.                         eggdrop(a, i);
  61.                
  62.                 int follow=beatdrop(a, b, 0);
  63.                 for (int x=1; x<b; x++) // Loop to find optimal floor from which to drop this floor
  64.                 {
  65.                         follow=Math.min(follow, beatdrop(a, b, x));
  66.                 }
  67.                
  68.                 return follow+1;
  69.         }
  70.        
  71.         private int beatdrop(int a, int b, int x) // Find worst case of smash or no smash for a given drop
  72.         {
  73.                 return Math.max(eggdrop(a-1, x), eggdrop(a, b-x-1));
  74.         }
  75.        
  76.         /**
  77.          * Outputs a decision tree
  78.          * Each line is spaced out by the number of drops that already happened.
  79.          * A number means "drop from this floor".  
  80.          *      The next line is what happens if there was no smash,
  81.          *      and the following line at that tablevel is what happens if there is a smash.
  82.          *
  83.          * n : Drop on floor n
  84.          * =n: Smashpoint is n
  85.          * ~n: Smashpoint is either n or n+1; drop at n
  86.          * ~n,m: Smashpoint is from n to m+1; dropscan from n to m
  87.          */
  88.         public void eggdrop_treewalk(int a, int b, int prefixed, int rebase, boolean smashfun)
  89.         {
  90.                 tabOut(prefixed);
  91.                
  92.                 // Note to me: a is at least 1, and b is... fuck b.
  93.                
  94.                 if (b<=0) // Not sure if this can happen, but I'm afraid to remove it >_>
  95.                 {
  96.                         System.out.println("="+rebase);
  97.                         return;
  98.                 }
  99.                
  100.                 if (b==1)
  101.                 {
  102.                         System.out.println("~"+rebase);
  103.                         return;
  104.                 }
  105.                
  106.                 if (a<=1)
  107.                 {
  108.                         System.out.println("~"+rebase+","+(rebase+b-1));
  109.                         return;
  110.                 }
  111.                
  112.                 eggdrop(a, b); // Makes sure the table is filled out
  113.                
  114.                 int follow=beatdrop(a, b, 0);
  115.                 int opx=0; // now we track optimal floor
  116.                 for (int x=1; x<b; x++)
  117.                 {
  118.                         int drop=beatdrop(a, b, x);
  119.                         if (drop<follow || (smashfun && drop==follow))
  120.                         {
  121.                                 follow=drop;
  122.                                 opx=x;
  123.                         }
  124.                 }
  125.                
  126.                 System.out.println(opx+rebase);
  127.                
  128.                 eggdrop_treewalk(a  , b-opx-1, prefixed+1, rebase+opx+1, smashfun); // If it doesn't break
  129.                 eggdrop_treewalk(a-1, opx    , prefixed+1, rebase      , smashfun); // If it does break
  130.         }
  131.        
  132.         private void tabOut(int spaces)
  133.         {
  134.                 for (;spaces>0;spaces--) System.out.print(" ");
  135.         }
  136. }
  137.  
  138. /*
  139. -----BEGIN PGP SIGNATURE-----
  140. Version: GnuPG v1.4.6 (GNU/Linux)
  141.  
  142. iD8DBQFJGpK3wwWAXIUwsAcRAv7KAJ47XjIYK4/iObsmuiWTZOxnMtujYwCeIe2U
  143. 3F/U5THbCw4z+DWueKd84e4=
  144. =MUj1
  145. -----END PGP SIGNATURE-----
  146. */
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: