hdu 2147 kiki game

来源:互联网 发布:怎么用淘宝卖东西 编辑:程序博客网 时间:2024/05/17 06:31

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<n,m<=2000). The input is terminated when n=0 and 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!

  博弈论:

P点:就是P个石子的时候,对方拿可以赢(自己输的)

N点:就是N个石子的时候,自己拿可以赢

现在关于P,N的求解有三个规则

(1):最终态都是P

(2):按照游戏规则,到达当前态的前态都是N的话,当前态是P

(3):按照游戏规则,到达当前态的前态至少有一个P的话,当前态是N

 

题意:

在一个m*n的棋盘内,从(1,m)点出发,每次可以进行的移动是:左移一,下移一,左下移一。然后kiki每次先走,判断kiki时候会赢(对方无路可走的时候)。

我们可以把PN状态的点描绘出来::

最后棋子一定到达(n,m)这个点,所以我们从这个点是必败点,从这里开始画出NP图。图:

NNNNNNNNNNNNNNNNNN

PNPNPNPNPNPNPNPNPN

NNNNNNNNNNNNNNNNNN

PNPNPNPNPNPNPNPNPN

NNNNNNNNNNNNNNNNNN

PNPNPNPNPNPNPNPNPN

NNNNNNNNNNNNNNNNNN

PNPNPNPNPNPNPNPNPN

NNNNNNNNNNNNNNNNNN

PNPNPNPNPNPNPNPNPN

NNNNNNNNNNNNNNNNNN

PNPNPNPNPNPNPNPNPN

根据该图可发现规律,当m为偶数时,都是必胜点,当m为奇数且n为奇数为必败点,当m为偶数且n为偶数时为必胜点,判断是否是偶数用按与操作。

 

#include<stdio.h> int main()   {   int n,m;    int k,l;   while(scanf("%d%d",&n,&m)&&!(m==0&&n==0))     {     k=n&1;     l=m&1;        if(l&&k)      printf("What a pity!\n");        else      printf("Wonderful!\n");     }   return 0;   }


 

0 0
原创粉丝点击