kiki’s game
Time Limit : 5000/1000ms (Java/Other) Memory Limit : 40000/10000K (Java/Other)
Total Submission(s) : 24 Accepted Submission(s) : 15
Problem Description
Recently kiki has nothing to do. While she is bored, an idea appears in his mind, she just playes the checkerboard game.The size of the chesserboard is n*m.First of all, a coin is placed in the top right corner(1,m). Each time one people can move the coin into the left, the underneath or the left-underneath blank space.The person who can’t make a move will lose the game. kiki plays it with ZZ.The game always starts with kiki. If both play perfectly, who will win the game?
Input
Input contains multiple test cases. Each line contains two integer n, m (0
Output
If kiki wins the game printf “Wonderful!”, else “What a pity!”.
Sample Input
5 3
5 4
6 6
0 0
Sample Output
What a pity!
Wonderful!
Wonderful!
巴什博弈:只有一堆n个物品,两个人轮流从这堆物品中取物,规定每次至少取一个,最多取m个。最后取光者得胜。
显然,如果n=m+1,那么由于一次最多只能取m个,所以,无论先取者拿走多少个,后取者都能够一次拿走剩余的物品,后者取胜。因此我们发现了如何取胜的法则:如果n=(m+1)r+s,(r为任意自然数,s≤m),那么先取者要拿走s个物品,如果后取者拿走k(≤m)个,那么先取者再拿走m+1-k个,结果剩下(m+1)(r-1)个,以后保持这样的取法,那么先取者肯定获胜。总之,要保持给对手留下(m+1)的倍数,就能最后获胜。那么这个时候只要n%(m+1)!=0,先取者一定获胜。
这个游戏还可以有一种变相的玩法:两个人轮流报数,每次至少报一个,最多报十个,谁能报到100者胜。
分析此类问题主要放法是:P/N分析:
***P点:即必败点,某玩家位于此点,只要对方无失误,则必败;
N点:即必胜点,某玩家位于此点,只要自己无失误,则必胜。*
*三个定理:
一、所有终结点都是必败点P(上游戏中,轮到谁拿牌,还剩0张牌的时候,此人就输了,因为无牌可取);
二、所有一步能走到必败点P的就是N点;
三、通过一步操作只能到N点的就是P点;*
如图就能很容易的看出,只要m或者n有一个是偶数先手就能必胜。
#include<stdio.h>
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),n,m)
{
if(n==0||m==0)
break;
else if(n%2==0||m%2==0)
printf("Wonderful!\n");
else
printf("What a pity!\n");
}
return 0;
}