Eggs dropping puzzle (2 Eggs, floors)

Source: Internet
Author: User

The topics are as follows:

You are given the eggs, and access to a 100-storey building. Both eggs is identical. The aim is for find out of the highest floor from which an egg would not be break when dropped out of a window from this floor. If an egg was dropped and does not break, it was undamaged and can be dropped again. However, once an egg was broken, that's it for that egg.

If an egg breaks is dropped from floor N, then it would also has broken from any floor above that. If an egg survives a fall and then it'll survive any fall shorter than that.

The question is: What strategy should do adopt to minimize the number egg drops it takes to find the solution? (And what's the worst case for the number of drops it would take?)

    • Question one: How to test when there is only one egg
      if we only have one egg, we know that once the eggs are broken, we have no eggs. So we're going to find this critital floor that makes the eggs just crumble, we can only start from the first layer up a layer of search.
      So in the worst case, we need to throw it 100 times to find it (either on the 100 floor, or on the 100 floor, not broken).

    • Question two: Now that we have two eggs, how do we test them? The
      we can start at the 50th level, so that we can reduce the size of our problem to half. There are two possibilities:
      (1) The eggs are broken, and we know that critical floor is below 50 floors. But at this point we still have an egg left, we have no other way, can only start from the first layer of a layer to look up. Worst case we need to check: 1 (corresponds to the first apprenticeship egg) + 49 = 50 times

      (2) The eggs are not broken. At this point we compare lucky, and then continue to use this egg for the two-point test.
      No matter what, we go to the two worst cases, that is, 50 times.

    • Question three: can do better
      so doing better is that we make the worst case, the number of throws is minimal. We can choose to throw eggs in the following way. The
      selects the strategy of 10floor. That is, choose the first egg: start from 10 layers, if broken, use the second egg check 1-9 layer. If it is not broken, continue with the egg from the 20 floor to start throwing, go on.
      Worst case scenario (i.e. the maximum number of eggs thrown):
      to 90 times the first egg is not broken. But it broke in 100 times. Then we use the second egg test starting from 91 layers, one layer at a test. Our number of times is: 10 (first egg) + 9 = 19 times. The
      good strategy is far better than the first one.

    • Question four: Can you do better?
      We can choose the minimization of maxmum regret policy.

The above approach is to use the arithmetic progression way to throw eggs. Now let's change our strategy. As long as our first egg is not broken, we will reduce the increase in the number of floors to throw eggs. This makes it possible that once our first egg is broken, the number of times required to test the second egg is decremented. For example, the first egg is broken on the first layer, and the first egg is broken in the second layer. The number of test times for the second egg that may correspond to the two causes is decremented.
How to find the first egg to start the layer of throwing eggs. We calculate it in the following way:
(1) The first egg starts down from floor N, if broken, use the second egg layer to check the front (n-1) floor.
(2) If the first egg is not broken, then we begin to throw it down from the 2n-1 layer. That means we're going up the n-1 layer and start throwing the first egg. If broken, check the front n-1 layer with the second egg. Without breaking, continue to throw the first egg down.
(3) The first egg is not broken, in the floor n + n-1 + n-2 = throw eggs at 3n-3.
Go down in turn.

We have the following formula:
n + (n-1) + (n-2) + (n-3) + (n-4) + ... + 1 >= 100
Then get:
N (n+1)/2 >= 100
Calculated by:
n = 13.651.
We take ceiling and get n = 14.
So we tested the situation as follows:

The worst case scenario is that we threw it 14 times. That's when we first threw the first egg, and the sad guy broke it. Then we can only start from the first layer, and then layer by level with a second egg check:
1 + 13 = 14.
Here we use dynamic programming to solve the problem.
n Eggs, K-storey.
A problem to be on the dynamic planning of this high-speed train, then the structure of the problem must have the following two excellent features.
(1) Optimal substructure
If an egg is thrown down from the X layer, two cases will appear:
–case 1: When the eggs are broken, we need to use the rest of the eggs n-1 (if we have n eggs) an egg to check the x-1 floor below.

in worst case.  111in {12..., k}}

(2) Overlapping sub-problems
This is easy to see.
Programming Implementation:
Recursive version:

# include <cstdio># include <climits>A utility function to get maximum of the integersintMaxintAintb) {return(A > B)? A:B; }/* Function to get minimum number of trails needed in worst case with n eggs andK Floors*/intEggdrop (intNintK) {//If there isNoFloors, then.NoTrials needed. ORifThere is//one floor, one trial needed.if(k = =1|| K = =0)returnK//We Need k trials forOne egg andK Floorsif(n = =1)returnKintMin = Int_max,x, Res;//Consider all droppings from1St Floor to kth floor and//returnThe minimum of theseValuesPlus1. for(x=1;x<= K;x+ +) {res = max (Eggdrop (n1,x-1), Eggdrop (N, Kx));if(Res < min) min = res; }returnMin +1;} /* Driver program to test to Pront printdups*/intMain () {intn =2, k =Ten;printf("\nminimum number of trials in worst case with %d eggs and"             "%d floors is %d \ n", N, K, Eggdrop (n, k));return 0;}

Dynamic planning:

# include <cstdio># include <climits>//A utility function to get maximum of the integersintMaxintAintb) {return(A > B)? A:B; }/ * Function to get minimum number of trails needed in worst case with n eggs and k floors * /intEggdrop (intNintK) {/ * A 2D table where entery eggfloor[i][j] would represent minimum number of trials needed for I eggs and J floors . */    integgfloor[n+1][k+1];intResintI, J, X;//We need one trial for one floor and0 trials for 0 floors     for(i =1; I <= N; i++) {eggfloor[i][1] =1; eggfloor[i][0] =0; }//We always need J trials for one egg and j floors.     for(j =1; J <= K; J + +) eggfloor[1][J] = j;//Fill Rest of the entries in table using optimal substructure    // Property     for(i =2; I <= N; i++) { for(j =2; J <= K; J + +) {Eggfloor[i][j] = Int_max; for(x =1; X <= J; X + +) {res =1+ MAX (eggfloor[i-1][x-1], eggfloor[i][j-x]);if(Res < EGGFLOOR[I][J]) eggfloor[i][j] = res; }        }    }//Eggfloor[n][k] holds the result    returnEGGFLOOR[N][K];}/ * Driver program to test to Pront printdups*/intMain () {intn =2, k = $;printf("\nminimum number of trials in worst case with%d eggs and"             "%d floors is%d \ n", N, K, Eggdrop (n, k));return 0;}

Operation Result:

Analysis:
Complexity of Time: O (nk^2)
Space complexity: O (NK)

Eggs dropping puzzle (2 Eggs, floors)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.