1. 程式人生 > >扔雞蛋問題 (動態規劃) (Balls)

扔雞蛋問題 (動態規劃) (Balls)

                                                      Balls

Time Limit: 1000MS   Memory Limit: 65536K
     

Description

The classic Two Glass Balls brain-teaser is often posed as:

"Given two identical glass spheres, you would like to determine the lowest floor in a 100-story building from which they will break when dropped. Assume the spheres are undamaged when dropped below this point. What is the strategy that will minimize the worst-case scenario for number of drops?"
Suppose that we had only one ball. We'd have to drop from each floor from 1 to 100 in sequence, requiring 100 drops in the worst case.
Now consider the case where we have two balls. Suppose we drop the first ball from floor n. If it breaks we're in the case where we have one ball remaining and we need to drop from floors 1 to n-1 in sequence, yielding n drops in the worst case (the first ball is dropped once, the second at most n-1 times). However, if it does not break when dropped from floor n, we have reduced the problem to dropping from floors n+1 to 100. In either case we must keep in mind that we've already used one drop. So the minimum number of drops, in the worst case, is the minimum over all n.

You will write a program to determine the minimum number of drops required, in the worst case, given B balls and an M-story building.

Input

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set consists of a single line containing three(3) decimal integer values: the problem number, followed by a space, followed by the number of balls B, (1 ≤ B ≤ 50), followed by a space and the number of floors in the building M, (1 ≤ M ≤ 1000).

Output

For each data set, generate one line of output with the following values: The data set number as a decimal integer, a space, and the minimum number of drops needed for the corresponding values of B and M.

Sample Input

4 
1 2 10 
2 2 100 
3 2 300 
4 25 900

Sample Output

1 4
2 14
3 24
4 10

推薦一篇寫的很好的部落格  https://blog.csdn.net/joylnwang/article/details/6769160

解題思路:
這是一個比較經典的 DP 問題,又叫做 “扔雞蛋問題”,假設 dp[n,w] 表示 n 層樓、w 個雞蛋時找到摔雞蛋不碎的最少判斷次數。則一個雞蛋從第 i 層扔下,如果碎了,還剩 w−1 個雞蛋,為確定下面樓層中的安全位置,還需要dp[i−1,w−1] 次(子問題);不碎的話,上面還有 n−i 層,還需要 dp[n−i,w]次(子問題,實體 n 層樓的上 n−i 層需要的最少判斷次數和一個新的問題的 n−i 層樓需要的最少判斷次數其實是一樣的)。

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define eps 1e-9
#define MAXN 1e5+10
int dp[1100][1100];//dp[i][j]:表示在 i 層樓 還有 j 個雞蛋的最小判斷次數
int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        int cnt,n,w;
        cin>>cnt>>w>>n;
        memset(dp,0,sizeof(dp));
        int i,j,k;
        for(i=1;i<=n;i++) // 就一個雞蛋,肯定是從第一層往上嘗試的
            dp[i][1]=i;
        for(i=1;i<=w;i++)  //就一層,扔1 次就可以
            dp[1][i]=1;
        for(i=2;i<=n;i++)
            for(j=2;j<=w;j++)
            {
                dp[i][j]=inf;  ////初始化為比較大的值
                for(k=1;k<=i;k++)
                    dp[i][j]=min(dp[i][j],max(dp[k-1][j-1],dp[i-k][j])+1);
//對小於i的每一層都遍歷一下,找最小次數的扔雞蛋樓層,例如現在為dp [i][j],在第i層,有j個雞蛋,然

//後從1遍歷到i-1層,找到次數最小的一層,比如扔到k層時,雞蛋碎了,那麼子問題轉化為你現在有j-1個雞

//蛋,有一個k-1層高的樓;當雞蛋沒碎時,子問題就轉化為你現在有j個雞蛋,有i-k層樓。
            }
        cout<<cnt<<" "<<dp[n][w]<<endl;
    }
    return 0;
}