-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathEgg Dropping Puzzle with DP
86 lines (52 loc) · 1.05 KB
/
Egg Dropping Puzzle with DP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <bits/stdc++.h>
using namespace std;
int fun1(int n,int k)
{
if(k==1 || k==0)
return k;
if(n==1)
return k;
int ans= INT_MAX;
for(int x=1;x<=k;x++)
{
ans=min(ans,max(fun1(n-1,x-1),fun1(n,k-x)));
}
return ans+1;
}
int fun(int n,int k)
{
int dp[n+1][k+1];
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;i++)
{
dp[i][0]=0;
dp[i][1]=1;
}
for(int j=1;j<=k;j++)
{
dp[1][j]=j;
}
int val=0;
for(int i=2;i<=n;i++)
{
for(int j=2;j<=k;j++)
{
dp[i][j]=INT_MAX;
for(int x=1;x<=j;x++)
{
val=1+max(dp[i-1][x-1],dp[i][j-x]);
dp[i][j]=min(dp[i][j],val);
}
}
}
return dp[n][k];
}
int main() {
int t; cin>>t;
while(t--)
{
int n,k; cin>>n>>k;
cout<<fun(n,k)<<endl;
}
return 0;
}