HDU 2147 kiki's game(博弈)
Time Limit: 1000MS | Memory Limit: 10000KB | 64bit IO Format: %I64d & %I64u |
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<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!
题意:有一个N*M的棋盘,硬币在(1,M),两个人轮流移动,每次可以将硬币向左或向下或向左下移动。
谁不能继续移动了谁输,赢了输出Wonderful!,输了输出What a pity!。
题解:左下角肯定是必败点P了,他的上,右和右上是必胜点N,依次递推可以得出:
PNPNPNPNP
NNNNNNNNN
PNPNPNPNP
NNNNNNNNN
PNPNPNPNP
NNNNNNNNN
PNPNPNPNP
NNNNNNNNN
PNPNPNPNP
NNNNNNNNN
PNPNPNPNP
NNNNNNNNN
PNPNPNPNP
即如果n和m都是奇数则为必败点,否则为必胜点。
注意:本题也叫“分”数问题,给定两个数每次将一个数变为0,另一个数分成两份给这两个数。
结论与本题一样,都是奇数则必败,根据(1,1)是P然后逆向递推即可得出结论。
顺便说一下另一种博弈:Chomp!博弈,即拿巧克力问题。结论:除了(1,1)都是必胜点。很好证明:
如果后手能赢,也就是说后手有必胜策略,使得无论先手第一次取哪个石子,后手都能获得最后的胜利。
那么现在假设先手取最右上角的石子(n,m),接下来后手通过某种取法使得自己进入必胜的局面。
但事实上,先手在第一次取的时候就可以和后手这次取的一样,进入必胜局面了,与假设矛盾。
以下是本题AC代码:
#include<iostream> #include<cstdio> using namespace std; int main() { int n,m; while(cin>>n>>m&&n&&m) { if(n&1&&m&1) puts("What a pity!"); else puts("Wonderful!"); } return 0; }