zoj1503 2010.7.28
One Person "The Price is Right"
--------------------------------------------------------------------------------
Time Limit: 1 Second Memory Limit: 32768 KB
--------------------------------------------------------------------------------
In the game show "The Price isRight", a number of players (typically 4) compete to get on stage byguessing the price of an item. The winner is the person whose guess is theclosest one not exceeding the actual price. Because of the popularity of theone-person game show "Who Wants to be a Millionaire", the AmericanContest Management (ACM) would like to introduce a one-person version of the"The Price is Right". In this version, each contestant is allowed G(1 <= G <= 30) guesses and L (0 <= L <= 30) lifelines. Thecontestant makes a number of guesses for the actual price. After each guess,the contestant is told whether it is correct, too low, or too high. If theguess is correct, the contestant wins. Otherwise, he uses up a guess.Additionally, if his guess is too high, a lifeline is also lost. The contestantloses when all his guesses are used up or if his guess is too high and he hasno lifelines left. All prices are positive integers.
It turns out that for a particular pair ofvalues for G and L, it is possible to obtain a guessing strategy such that ifthe price is between 1 and N (inclusive) for some N, then the player canguarantee a win. The ACM does not want every contestant to win, so it mustensure that the actual price exceeds N. At the same time, it does not want thegame to be too diffcult or there will not be enough winners to attractaudience. Thus, it wishes to adjust the values of G and L depending on the actualprice. To help them decide the correct values of G and L, the ACM has asked youto solve the following problem. Given G and L, what is the largest value of Nsuch that there is a strategy to win as long as the price is between 1 and N(inclusive)?
Input
The input consists of a number of cases.Each case is specified by one line containing two integers G and L, separatedby one space. The end of input is specified by a line in which G = L = 0.
Output
For each case, print a line of the form:
Case c: N
where c is the case number (starting from1) and N is the number computed.
Sample Input
3 0
3 1
10 5
7 7
0 0
Sample Output
Case 1: 3
Case 2: 6
Case 3: 847
Case 4: 127
在一个猜商品价格的游戏中,每个参赛者有G(1<=G<=30)个猜的机会(guess),L(0<=L<=30)条生命线(lifeline)。这个参赛者能够对物品的正确价格猜G次。每次他猜了以后会被告知他到底是猜对了还是太高了,或者太低了。假如猜对了,他就赢了。猜错了的话,他将lose一个guess,并且如果他猜得太高,还会lose一个lifeline。如果参赛者在guess用玩之后仍未猜中,或者他这一次猜的价格太高而他没有lifeline,他就输了。所有价格都是整数。
我们发现对于特定的G和L,参赛者能够找到一个必胜策略,只要价格在1到N之间(包括N)。任务:找出相对应的N。输入L,G,对于每一个数据输出对应的N
输入以0 0结束。
f[g,l]:g次猜的机会 ,l条生命线时可以猜的次数
递推函数:f(g,l)=f(g-1,l-1)+1+f(g-1,l)
初始化:f(i,0)=i (0<=i<=g)
f(0,i)=0 (0<=i<=l)
递推函数含义:
1、任意猜一个数m;
2、if m>price 在m前还有f(g-1,l-1)个值能猜;
3、if m<price 在m后还有f(g-1,l)个值能猜;
4、在加上已猜过m,所以一共有f(g-1,l-1)+1+f(g-1,l)个值能猜。
#include <cstdio>
#include <cstring>
#define MAX 35
int f[MAX][MAX];
int g,l;
int main()
{
int cas=0;
while (scanf("%d %d",&g,&l),!((g==0)&&(l==0)))
{
memset(f,0,sizeof(f));
for(int i=0;i<=g;i++)
f[i][0]=i;
for(int i=1;i<=g;i++)
for(int j=1;j<=l;j++)
f[i][j]=f[i-1][j-1]+f[i-1][j]+1;
int ans=-1;
for(int i=0;i<=g;i++)
for(int j=0;j<=l;j++)
if (f[i][j]>ans)
ans=f[i][j];
cas++;
printf("Case %d: %d\n",cas,ans);
}
return 0;
}