You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int dp[N][N];
//在n层楼剩余m个鸡蛋返回最少的尝试次数
int f(int n, int m) {
if (m == 1) return n; //如果只有一个鸡蛋那么需要枚举n层楼
if (n == 0) return 0; //如果没有楼,就不用再试了
if (dp[n][m]) return dp[n][m]; //计算过就返回
int ret = INT_MAX;
for (int i = 1; i <= n; i++) //在当前场景下,尝试每一层楼进行扔鸡蛋,把所有可能计算出来,挑一个尝试次数最小的
// f(n - i, m):鸡蛋没碎楼层太矮继续在i+1~n 这n-i层楼进行尝试,还有m个鸡蛋
// f(i - 1, m - 1):鸡蛋碎了楼层太高继续在1~i-1层楼进行尝试
// +1:代价是使用了一个次数
ret = min(ret, max(f(n - i, m), f(i - 1, m - 1)) + 1); //
return dp[n][m] = ret;
}
int g(int n, int m) {
for (int i = 1; i <= n; i++) dp[i][1] = i;
for (int j = 1; j <= m; j++) dp[0][j] = 0;
for (int i = 1; i <= n; i++)
for (int j = 2; j <= m; j++) {
dp[i][j] = INT_MAX;
for (int k = 1; k <= i; k++)
dp[i][j] = min(dp[i][j], max(dp[i - k][j], dp[k - 1][j - 1]) + 1);
}
return dp[n][m];
}
int main() {
int n, m;
// cin >> n >> m;
// cout << f(n, m) << endl
// << g(n, m) << endl;
cout << f(15, 3) << endl;
return 0;
}