Egg Dropping Puzzle

#include <bits/stdc++.h>

using namespace std;

int max(int a,int b)
{
    return (a > b) ? a : b;
}

int min(int a,int b)
{
    return (a < b) ? a : b;
}

int eggDrop(int n,int k)
{
    if( k == 0 || k == 1)
        return k;
    if( n == 1)
        return k;
    int minn = INT_MAX,res;

    for(int f = 1; f <= k; f++)
    {
        //如果在这一层蛋破了,那我们的蛋就少一个,继续测试f-1的楼层
        //如果蛋没破,那我们还是有n个蛋,楼层就剩k-f
        res = max(eggDrop(n-1,f-1),eggDrop(n,k-f));
        minn = min(res,minn);
    }
    return minn+1;
}

int eggDrop1(int n,int k)
{
    int res[n+1][k+1];
    
    //当k == 0 || k==1
    for(int i = 1; i <=n; i++ )
    {
        res[i][0] = 0;
        res[i][1] = 1;
    }

    //n==1
    for(int j = 1; j <= k; j++)
        res[1][j] = j;
    
    for(int i = 2; i <= n; i++)
    {
        for(int j = 2; j <= k; j++)
        {
            res[i][j] = INT_MAX;
            for(int f = 1; f <= j; f++)
            {
                int r = 1+max(res[i-1][f-1],res[i][j-f]);
                res[i][j] = min(res[i][j],r);
            }
        }
    }
    return res[n][k];
}



int main()
{
    cout << eggDrop1(2,36);
    // cout << eggDrop(2,36) << endl;
    return 0;
}

 

点赞