zoukankan      html  css  js  c++  java
  • URAL1223——DFS—— Chernobyl’ Eagle on a Roof

    Description

    Once upon a time an Eagle made a nest on the roof of a very large building. Time went by and some eggs appeared in the nest. There was a sunny day, and Niels Bohr was walking on the roof. He suddenly said: “Oops! All eggs surely have the same solidity, thus there is such non-negative number E that if one drops an egg from the floor number E, it will not be broken (and so for all the floors below the E-th), but if one drops it from the floor number E+1, the egg will be broken (and the same for every floor higher, than the E-th).” Now Professor Bohr is going to organize a series of experiments (i.e. drops). The goal of the experiments is to determine the constant E. It is evident that number E may be found by dropping eggs sequentially floor by floor from the lowest one. But there are other strategies to find E for sure with much less amount of experiments. You are to find the least number of eggs droppings, which is sufficient to find number E for sure, even in the worst case. Note that dropped eggs that are not broken can be used again in following experiments.
    The floors are numbered with positive integers starting from 1. If an egg has been broken being dropped from the first floor, you should consider that E is equal to zero. If an egg hasn’t been broken even being dropped from the highest floor, consider that E is also determined and equal to the total number of floors.

    Input

    Input contains multiple (up to 1000) test cases. Each line is a test case. Each test case consists of two numbers separated with a space: the number of eggs, and the number of floors. Both numbers are positive and do not exceed 1000. Tests will end with the line containing two zeroes.

    Output

    For each test case output in a separate line the minimal number of experiments, which Niels Bohr will have to make even in the worst case.

    Sample Input

    inputoutput
    1 10
    2 5
    0 0
    
    10
    3
    

    大意:摔蛋,每一个蛋的坚固系数相同,问最少的摔蛋次数,不断二分,直到最后一个不能摔碎,考虑到二分,而且塔的高度不超过1000,所以最多摔碎10个蛋1024就行了

    定义 dp[i][j] 表示用了i个蛋,需要测j楼层的摔蛋次数

    动态转移方程  dp[i][j] = min(dp[i-1][j-1],dp[i][n-j]) 摔碎和没摔碎

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    int dp[15][1110];
    const int inf = 0x3f3f3f3f;
    int dfs(int x,int y)
    {
        if(dp[x][y])
            return dp[x][y];
        if(x == 1){
            dp[x][y] = y;
            return y;
        }
        if(y <= 2){
            dp[x][y] = y;
            return y;
        }
        int min1 = inf;
        for(int i = 2; i < y; i++){
            int max1 = max(dfs(x,y-i)+1,dfs(x-1,i-1)+1);
            min1 = min(min1,max1);
        }
            dp[x][y] = min1;
            return min1;
    }
    int main()
    {
        int n,m;
        while(~scanf("%d%d",&n,&m)&&(n&&m)){
            if(n > 10)
                n = 10;
           printf("%d
    ",dfs(n,m));
        }
        return 0;
    }
    

      

  • 相关阅读:
    《程序猿面试宝典3》大量错误(50+)纠正表
    STM32定时器的预装载寄存器与影子寄存器之间的关系【转】
    Linux虚拟内存和物理地址的理解【转】
    UNIX系统的显示时间何时会到尽头
    assert函数用法总结【转】
    Sizeof与Strlen的区别【转】
    C语言预处理器命令详解【转】
    C#预处理器指令【转】
    I2C总线信号时序总结【转】
    用状态机实现键盘消抖【转】
  • 原文地址:https://www.cnblogs.com/zero-begin/p/4498318.html
Copyright © 2011-2022 走看看