码迷,mamicode.com
首页 > 其他好文 > 详细

URAL1223——DFS—— Chernobyl’ Eagle on a Roof

时间:2015-05-12 20:57:07      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:

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\n",dfs(n,m));
    }
    return 0;
}

  

URAL1223——DFS—— Chernobyl’ Eagle on a Roof

标签:

原文地址:http://www.cnblogs.com/zero-begin/p/4498318.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!