[COMP2119] Searching - Building and Egg Problem
Description
There is a building with floors and you have eggs. Determine the lowest floor thrown from which an egg will break. If an egg is broken, it cannot be used again, otherwise, it can.
What is the minimum number of probes needed?
Let be the minimum probe needed for floors and eggs. Consider the first floor to search, denote it as , then
where the first term stands for the case that the egg is broken, and the second for not broken.
The answer should be . The initial conditions are
Code:
scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) for (int j=1;j<=m;j++) f[i][j]=2147483647; for (int i=1;i<=n;i++){ f[i][1]=i; for (int j=2;j<=m;j++){ for (int k=1;k<=i;k++) if (1+max(f[k-1][j-1],f[i-k][j])<f[i][j]){ //记得初始化 f[i][j]=1+max(f[k-1][j-1],f[i-k][j]); fir[i][j]=k; } } } printf("%d",f[n][m]);
What are the optimal strategy and the asymptotic evaluation of the minimum number of probes?
Case 1, : The only way is to do linear search from lower to higher since there is only 1 egg.
Case 2, : binary search. (not proven to be the optimal strategy but is consistent with the numerical result)
Case 3, :
Let be the number of probes the optimal strategy takes.
First, consider , i.e. try to find .
Square search is a feasible strategy, so .
Now, try to find the lower bound of :
Assume floors the optimal strategy probes forms an increasing sequence .
According to the above upper bound, .
If : we have the following deduction
This means, there exists one gap between two consecutive probes, say and , that is large than . Then if we
create a situation where the critical floor is in this gap, the probe will break one egg. So for the remaining floors in
between, we can only do a linear search since there is only one egg left. Then the total number of probes will be larger than
, which contradicts the assumption.
Therefore,
The upper bound and lower bound match, meaning that .
With being the base case, we can do a two-variable mathematical induction on with the following induction formula (which is the same as the DP):
The conclusion is that for . But actually it doesn't match that well with the numerical result...
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构