动态规划(from geeksforgeeks)

11.扔鸡蛋问题(Egg Dropping Puzzle)

int dp[11][51];
void minTrials(){
    for (int i = 1; i <= 10; ++i){
        dp[i][1] = 1;
    }
    for (int j = 1; j <= 50; ++j){
        dp[1][j] = j;
    }
    for (int i = 2; i <= 10; ++i){
        for (int j = 2; j <= 50; ++j){
            dp[i][j] = 1 + dp[i][j - 1];
            for (int k = 2; k <= j; ++k){
                dp[i][j] = min (dp[i][j], 1 + max (dp[i - 1][k - 1], dp[i][j - k]));
            }
        }
    }
}
int main() {
    int t;
    cin >> t;
    minTrials();
    while (t--){
        int n, k;
        cin >> n >> k;
        cout << dp[n][k] << endl;
    }
    return 0;
}

你可能感兴趣的:(动态规划)