Egg dropping puzzle)

Source: Internet
Author: User

The typical dynamic planning problem is as follows:
If you have 2 eggs and a 36-storey building, now you want to know under which floor the eggs will not be broken, how should we use the least number of tests to solve the problem on any floor with answers.

  • If you drop the egg from a certain level of building and it is not broken, you can continue to use the egg.
  • If the eggs are broken, you can use the eggs to test to reduce the number
  • All eggs are of the same quality (they will be broken above the same floor)
  • For an egg, if it is broken when it is dropped on floor I, the egg will be broken for any floor not less than I.
  • If the eggs are not broken when floor I is dropped, the eggs will not be broken for any floor not larger than I.
  • The eggs are not necessarily intact when they are dropped from the second layer. The eggs may not be broken even if they are dropped from the second layer.

In fact, our ultimate goal is to find two consecutive floors I, I + 1 in the floor I eggs are not broken, in the floor I + 1 eggs are broken, the key to the problem is, before testing, you don't know where the eggs will be broken. What you need to find is a testing solution, no matter where the eggs will be broken, at most M tests are required. In all these test schemes, the m value is the minimum.

We have no choice but to test it from the first floor until the eggs on the first floor are broken, the floor where the eggs are broken is I (or the eggs are not broken until the top floor). Other test schemes are not feasible, because if 1st tests were to drop eggs on any I> 1 floor, if the eggs were broken, you could not be sure whether the I-1 layer would also break the eggs. Therefore, the worst case for an egg is that the number of floors that break the egg is I> = 36. At this time, we need to test each floor for a total of 36 times to find the final result, therefore, the minimum number of tests that can solve the problem of 36 floors is 36.

For 2 eggs and 36 floors, you may consider throwing one at Layer 3. If this is broken, you will go from Layer 3 to Layer 3, test with 2nd eggs until you find the answer. If 1st eggs are not broken, you can still test them with 1st eggs on the 27 layers ~ Layer 26: Use 2nd eggs for testing in sequence. If not broken, use 1st eggs for testing on layer 32 ,......, Perform this operation (similar to binary search ). The worst case of this solution is that when the result is layer 17/18, You need to test 18 times to find the final answer, the number of tests to solve the 36-floor problem is 18.

Compared with 1 egg to solve the problem of 36 floors, the number of tests is halved, but 18 does not ensure that the problem of 2 egg and 36 floors is minimized (the actual minimum value is 8 ).

We can describe this problem as W (n, k), where N represents the number of eggs that can be used for testing, and K represents the number of floors to be tested. For question W (1st), we can consider dropping eggs on layer I (I can be 1 ~ K of any value), if broken, then we need to use 2nd eggs, solve the sub-problem from layer 1st to the floor of the I-1 W (1, I-1 ), if the eggs are not broken, we need to use them to solve the sub-problem w (36th-I) from the I + 1 layer to the layer, we can get a number of tries p and q respectively. We take the larger ones (assuming p) from these two attempts and add them to the one where the 1st tests were executed at layer I, then p + 1 is the first time the eggs are still in the I layer to solve the minimum number of tests required for W () Ti. For the first time, we can put the eggs on any of the 36 floors, so we can get the testing times of the 36 medium solution t {T1, T2, T3, ......, T36}, in these results, we select the smallest ti so that for the arbitrary value TJ (1 <= j <= 36, J! = I), where all Ti <= TJ exists, Ti is the answer to this question. The formula is described as W (n,
K) = 1 + min {max (w (n-1, x-1), w (n, k-x)}, X in {2, 3, ......, K}, where X is the floor position of the first test.

Among them, w (1, K) = K (equivalent to 1 egg test K floor problem), w (0, K) = 0, w (n, 0) = 0

So before calculating W (), we need to calculate all W ),......, W (), w ),......, The values w () can be implemented using a recursive method. The Code is as follows:

unsigned int DroppingEggsPuzzle(unsigned int eggs, unsigned int floors){unsigned int i, j, k, t, max;unsigned int temp[eggs + 1][floors + 1];for(i = 0; i < floors + 1; ++i){temp[0][i] = 0;temp[1][i] = i;}for(i = 2; i < eggs + 1; ++i){temp[i][0] = 0;temp[i][1] = 1;}for(i = 2; i < eggs + 1; ++i){for(j = 2; j < floors + 1; ++j){for(k = 1, max = UINT_MAX; k < j; ++k){t = temp[i][j - k] > temp[i - 1][k -1] ?  temp[i][j - k] : temp[i - 1][k -1];if(max > t){max = t;}}temp[i][j] = max + 1;}}return temp[eggs][floors];}

The space complexity of this algorithm is O (NK), and the time complexity is O (NK ^ 2). For large-scale problems, both space and time complexity are considerable.

This algorithm can calculate the minimum number of tests for the W () problem is 8, but it cannot provide a specific solution to solve the problem of 36 floors with 2 eggs, here I will provide a test scheme:

  • Use the first egg to test on Layer 8, 15, 33, and 35, respectively.
  • If the eggs are broken on a certain layer (for example, 26 layers), test them from bottom to top in the previous test, for example (, 23, 24, 25) until the qualified floors are found.
  • If the eggs are not broken in the Layer 2 test, use the eggs to test again on the layer 3.

This solution ensures that no matter how many floors meet the conditions, you can find the answer after a maximum of 8 tests. For example, if the target floor is 28, the test sequence of this solution is 8 or 15,, 28, a total of 7 tests, interested readers can try other situations.

This solution is elegant in solving the W () problem, but it hides a great mystery, that is, the problem we usually see is W ), W (), I don't know whether the readers have considered it. Why not let us calculate 2 eggs to Test 36 floors, not 35 or 37 floors? The following is a recursive result table that uses the previous algorithm to solve the W () Problem (where, the rows represent the number of floors 1 ~ 50, the number of eggs in the column is 1 ~ 4), we will find that, w () = 8, w () = 9, is it true that 2 eggs are tested for 8 times and a maximum of 36 floors can be solved, what can I do for layer 37?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 6 6 6 6 6 6 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 10 10 10 10 10
1 2 2 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7
1 2 2 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6

The following is a problem: N eggs, tested m times (abbreviated as D (n, m), can solve the problem of several floors at most. By observing the recursive result table, we can draw the following conclusions:

  1. D (1, m) = m
  2. D (n, n) = 2 ^ n-1
  3. D (n, m) {M <= n} = D (M, m)

For the second point, take D (1st) as an example. We dropped eggs on the eighth floor times. If the eggs are broken, we dropped the eggs on the fourth floor for the second time. Otherwise, we dropped the eggs on the 12th floor, if you drop eggs on the 4th floor, you can drop the eggs on the 2nd or 6th floor. In this way, you can find the answer floor in the same way as the second-level search. For example, if the answer floor is 5, the test sequence is 8, 4, 6, and 5.

For the third point, if you have five eggs for three tests, even if the three tests are broken, the remaining two eggs will not work, so d (5, 3) = D (3, 3)

After finding these relationships, we seem to find a solution to solve the maximum number of floors that can be solved by testing n eggs m times. For D (n, m) {n <m}, for the maximum number of floors that can be tested, we can construct such a scenario to keep the first egg on floor I, the maximum number of floors that can be solved from layer I + 1 to layer K is D (m-1), and from layer 1st to layer I-1 is D (n-1, m-1) the maximum number of floors that can be solved, and the recursive relationship D (n, m) = D (n-1 M-1) + 1 + d (m-1) is obtained ), then, based on the formula D (m-1), D (N-1 m-1), the three calculation conditions (n
Until 1, or M <= N), and then accumulate the values of D (n, m). The Code is as follows:

unsigned int DroppingMax(unsigned int eggs, unsigned times){if(eggs == 1){return times;}if(eggs >= times){return (unsigned int)pow(2, times) - 1;}return DroppingMax(eggs, times -1) + DroppingMax(eggs -1, times - 1) + 1;}

Based on this algorithm, we can conclude that D () = 15, D () = 36, that is, 2 eggs can be tested 5 times at most to solve the problem of 15 floors, A maximum of 36 floors can be solved after eight tests. It can be seen that the people who come up with this question are not just looking for two floors to play with us, but the results after carefully studying this question. With this powerful tool, our solution to the problem of throwing eggs will be greatly simplified. For N eggs to solve the problem of K floors, we only need to find the value m, so that D (n, m-1 k <= D (n, m), the Code is as follows:

unsigned int DroppingEggsPuzzle2(unsigned int eggs, unsigned int floors){unsigned int times = 1;while(DroppingMax(eggs, times) < floors){++times;}return times;}

The time and space complexity of this algorithm is not well analyzed, but it is better than the traditional DP algorithm. If you are interested, you can think about it and test 10 eggs on my machine, the second method is 5000 times faster than the first one! Note that algorithm 2 is also a dynamic planning problem, so we can use an N * M matrix to store intermediate results in the computing process, and the efficiency of the algorithm can be greatly improved!

No matter algorithm 1 or algorithm 2, it does not show how to use N eggs to pass M tests to solve the problem of K floors. I will give an idea based on algorithm 2. For the number of tests that meet the condition d (m-1) <k <= D (n, m) m, convert D (n, m), and D (n, m) according to the formula D (n, m) = D (n-1 M-1) + 1 + d (n, s-1, in the process of expansion, we must strictly follow the iteration sequence in the formula, that is, first D (N-1 m-1), then 1, and then D (m-1). The order cannot be messy, then compare the two results, for example

D (3, 5) = D (1, 3) + 1 [2] + d (1, 2) + 1 [3] + D (2, 2) + 1 [1] + d (1, 2) + 1 [3] + D (2, 2) + 1 [2] + d (3, 3)
D (3, 4) = D (1, 2) + 1 [2] + D (2, 2) + 1 [1] + d (3, 3)

Each of these independent 1 represents an independent test. The brackets behind these 1 represent the number of independent tests, which are related to the timing of separation from the formula, the value of the first isolated 1 is [1], and the value of the second isolated 1 is [2]. the objective of these 1 s is to break down the K-floor into several sub-parts that can be directly calculated. Let's take out the different parts of the two: D () + 1 [2] + d () + 1 [3] + d () + 1 [1], this part indicates that by adding a test, we obtain additional testing capabilities, and by modifying this part, the sum of this part is equal to K-D (n, s-1 ), then combine the modified part with the same part of the two to form new results. These results correspond to the floor test plan from bottom to top.

In the above example, we know that D (3, 4) = 14, D (3, 5) = 25. For 14 <k <= 25, we subtract 14 from K to obtain the value to be constructed, keep the formula on the right as much as possible, and only change the formula on the left. For example, for k = 15, different parts can be replaced by 1, and for k = 16, D () + 1 can be replaced, for k = 18, you can replace it with D () + 1. For k = 21, you can replace it with D () + 1 + d () + 1. Taking 21 as an example, we combine the transformation results with the same parts of D (3, 4), D (3, 5) to form

D (1, 2) + 1 [2] + D (2, 2) + 1 [1] + d (1, 2) + 1 [3] + D (2, 2) + 1 [2] + d (3, 3)
The following figure shows how to use 3 eggs for 5 tests to solve the problem of 21 floors. The rule here is that for an independent test, if the test is broken, perform subsequent tests on the lower floor. If the tests are not broken, perform subsequent tests on the higher floor. The brackets indicate the floor/floor intervals for the tests.

In fact, there are more than one test solution that meets the conditions of D (m-1) <k <D (n, m.
Postscript:

  • This is an article by cool people outside China. The theoretical analysis of the problem of throwing eggs is amazing. Interested readers can take a look and dig deeper into this question.
  • I have not been able to provide a mathematical proof of the premise of algorithm 2. I have not read the article, but it is too long.
  • Based on the recursive relationship of D (n, m), we can obtain the general formula of this series.
  • The problem of throwing eggs is not so much a dynamic planning problem as a mathematical problem in a specific scenario. The value of a program in this problem lies in the conclusion of verification.


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.