English 中文(简体)
2-Egg Puzzle
原标题:Generalised Two-Egg Puzzle

问题说明:

Suppose that we wish to know which stories in a N-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions: An egg that survives a fall can be used again.

  • A broken egg must be discarded.
  • The effect of a fall is the same for all eggs.
  • If an egg breaks when dropped, then it would break if dropped from a higher window.
  • If an egg survives a fall then it would survive a shorter fall.
  • It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the Nth-floor windows do not cause an egg to break.

Given an N story building and a supply of d eggs, find the strategy which minimizes (in the worst case) the number of experimental drops required to determine the breakfloor.


I have seen and solved this problem for 2 eggs where answer comes out to be 14 for N=100. I tried to understand the generalized solution from wiki using DP but couldn t Understand what are they trying to do. Please tell how they arrived at the DP and how it is working ?

http://www.un.org。

http://www.scribd.com/api_user_11797_WebGuru/d/7217370-Egg-Puzzle-Generalized-Solution-for-Any-Number-of-Eggs> 可以用 d和鸡蛋测试的最高层:

f[d,e] = f[d-1,e] + f[d-1,e-1] + 1

复发是罚款,但不能理解它是如何衍生的?

我不清楚这一解释。 ......我只想有人向我解释这种重复。

最佳回答

(1) 考虑先出产卵的情况。 那么,如果和只有位于多数[d-1, e-1]时,你才能确定该底线。 因此,您的起点可能高于[d-1, e-1]+1(当然开始较低)。

如果您的首例辍学就没有打破蛋, 页: 1

www.un.org/spanish/ecosoc 因此,你所能做的最佳选择是开始在地表[d-1, e-1]+1(因为(1))上丢弃鸡蛋,而且你可以达到高于((2))的[d-1, e]层。 ......

f[d, e] = f[d-1, e-1] + 1 + f[d-1, e]
问题回答

http://algohub.blogspot.in/4005/egg-drop-puzzle.html” rel=“nofollow” http://algohub.blogspot.in/4005/egg-drop-puzzle.html

I believe it is self-explanatory..please feel free to ask if any part is not clear..will be happy to explain

这个问题可以通过三种办法解决(我知道):

  1. Dynamic Programming
  2. Solution using Binary Search Tree
  3. Solution by obtaining the direct mathematical formula for maximum number of floors that can be tested or covered with given number of eggs and given number of drops

我首先要界定随后进行分析时所用的一些符号:

e = number of eggs
f = number of floors in building
n = number of egg drops 
Fmax(e, n) = maximum number of floors that can be tested or covered with e eggs and n drops

The crux for dynamic programming approach lies in following recursive formula for Fmax:

Fmax(e, n) = 1 + Fmax(e-1, n-1) + fmax(e, n-1)

获取Fmax直接数学公式的关键在于:

Fmax(e, n) = { ∑Fmax(e-1,i) for i = 1 to n } - Fmax(e-1, n) + n 

解决这一问题还有可能采用 Bin树的替代解决办法。 为了便利我们的分析,让我们稍作修改地利用《巴塞尔协议》:

1.    If egg breaks then child node is drawn on left down side
2.    If egg does not break then child node is drawn straight down side

如果我们以上述代表制取出最低生活标准,那么生育补贴就意味着鸡蛋的数量。

任何具有一定数量的des子的生物浓缩系数,均以上述代表性计算,并受《生物浓缩和生物浓缩法》的束缚;= e(卵数)是一种解决办法,但可能不是最佳解决办法。

因此,获得最佳解决办法相当于在最低高度受限制的《基本生活标准》中获取节点安排:

关于以上所有三个方法的详情,请在

这一问题并不高于应放弃低层卵的问题,而是将下降的数量降至最低。

  • Suppose, we have n eggs and k floors then,
  • Base Case:
    • When floor is 1 then, MinNoOfDrops(n, 1)=1
    • And when egg is 1 the, MinNoOfDrops(1, k)=k
  • Generailsed Solution:
  • MinNoOfDrops(n, k) = 1 + min{ max(MinNoOfDrops(n − 1, x − 1),
    MinNoOfDrops(n,k − x)) } , x = 1, 2, ..., k

<>Dynamic Planning Algorithm:

  • 编制表格(合计+) (1) X(总数+1)

  • Base Case: When egg is zero or one then, set for floor i, table[0][i] = 0; and table[1][i] = i

  • Base Case: Floor is zero or one then, set for egg j, table[j][0] = 0 and table[j][1] = 1

  • 鸡蛋,从2到总数

    • Iterate floor j from 2 to total_floors
      • Set table[i][j] = INFINITY
      • Iterate floor k from 1 to j
      • Set maxDrop = 1 + max(table[i-1][k-1], table[i][j-k])
      • If table[i][j] > maxDrop then
        • Set table[i][j] = maxDrop

public class EggDroppingPuzzle {

    /** Not efficient  **/
    public static int solveByRecursion(int totalEggs, int totalFloors) {

        /** Base Case: When no floor **/
        if (totalFloors == 0) {
            return 0;
        }

        /** Base case: When only one floor **/
        if (totalFloors == 1) {
            return 1;
        }

        /** Base case: When only one eggs, then we have to try it from all floors **/
        if (totalEggs == 1) {
            return totalFloors;
        }

        int minimumDrops = Integer.MAX_VALUE;
        /** Now drop a egg from floor 1 to totalFloors **/
        for (int k = 1; k <= totalFloors; k++) {

            /** When an egg breaks at kth floor **/
            int totalDropWhenEggBreaks = solveByRecursion(totalEggs - 1, k - 1);

            /** When egg doesn t break at kth floor **/
            int totalDropWhenEggNotBreaks = solveByRecursion(totalEggs, totalFloors - k);

            /** Worst between above conditions **/
            int maxDrop = Math.max(totalDropWhenEggBreaks, totalDropWhenEggNotBreaks);


            /** Minimum drops for all floors **/
            if (minimumDrops > maxDrop) {
                minimumDrops = maxDrop;
            }
        }

        return minimumDrops + 1;
    }

    public static int solveByByDP(int totalEggs, int totalFloors) {
        int[][] table = new int[totalEggs + 1][totalFloors + 1];

        /** Base Case: When egg is zero or one **/
        for (int i = 0; i < totalFloors + 1; i++) {
            table[0][i] = 0;
            table[1][i] = i;
        }

        /** Base case: Floor is zero or one **/
        for (int j = 0; j < totalEggs + 1; j++) {
            table[j][0] = 0;
            table[j][1] = 1;
        }

        /** For floor more than 1 and eggs are also more than 1 **/
        for (int i = 2; i < totalEggs + 1; i++) {
            for (int j = 2; j < totalFloors + 1; j++) {

                table[i][j] = Integer.MAX_VALUE;
                for (int k = 1; k <= j; k++) {

                    /** When an egg breaks at kth floor **/
                    int totalDropWhenEggBreaks = table[i - 1][k - 1];

                    /** When egg doesn t break at kth floor **/
                    int totalDropWhenEggNotBreaks = table[i][j - k];

                    /** Worst between above conditions **/
                    int maxDrop = 1 + Math.max(totalDropWhenEggBreaks, totalDropWhenEggNotBreaks);

                    /** Minimum drops for all floors **/
                    if (maxDrop < table[i][j]) {
                        table[i][j] = maxDrop;
                    }
                }
            }
        }

        return table[totalEggs][totalFloors];
    }
}




相关问题
How to add/merge several Big O s into one

If I have an algorithm which is comprised of (let s say) three sub-algorithms, all with different O() characteristics, e.g.: algorithm A: O(n) algorithm B: O(log(n)) algorithm C: O(n log(n)) How do ...

Grokking Timsort

There s a (relatively) new sort on the block called Timsort. It s been used as Python s list.sort, and is now going to be the new Array.sort in Java 7. There s some documentation and a tiny Wikipedia ...

Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Enumerating All Minimal Directed Cycles Of A Directed Graph

I have a directed graph and my problem is to enumerate all the minimal (cycles that cannot be constructed as the union of other cycles) directed cycles of this graph. This is different from what the ...

Quick padding of a string in Delphi

I was trying to speed up a certain routine in an application, and my profiler, AQTime, identified one method in particular as a bottleneck. The method has been with us for years, and is part of a "...

热门标签