-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheggs_floors.c
76 lines (73 loc) · 1.51 KB
/
eggs_floors.c
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
//http://ide.geeksforgeeks.org/ignwg5
#include<stdio.h>
#include<stdlib.h>
int eggdroping(int eggs,int floors)
{
if(eggs==1)
{
return floors;
}
int j=1,m,k=0,l,a,i;
int dp[eggs+1][floors+1];
for(i=0;i<=floors;i++)
dp[0][i]=0,dp[1][i]=i;
for(i=0;i<=eggs;i++)
dp[i][0]=0;
j=1;
while(j<=floors)
{
for(i=0;i<k && j<=floors;i++)
{
dp[2][j]=k;
j++;
}
k++;
}
for(i=3;i<=eggs;i++)
{
a=1;
j=1;
k=0;
while(j<=floors)
{
l=1;
while(dp[i-1][l]!=a)
{
l++;
}
for(k=0;k<dp[1][l] && (j+k)<=floors;k++)
{
dp[i][j+k]=a;
}
j=j+k;
a=a+1;
}
}
//uncomment the below to see the dp table
/*for(i=0;i<=eggs;i++){
for( j=0;j<=floors;j++){
if(j>9 && dp[i][j]<10)
{printf(" ");}
printf("%d ",dp[i][j]);
}
printf("\n");
} printf("\n"); printf("\n");*/
return dp[eggs][floors];
}
int main(){
int t,n;
scanf("%d",&t);
n=t;
int res[t],i=0;
while(t--){
int eggs,floors;
scanf("%d %d",&eggs,&floors);
int t=0;
res[i]=eggdroping(eggs,floors);i++;
}
for(i=0;i<n;i++)
{
printf("%d\n",res[i]);
}
return 0;
}