kiki's game
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 40000/1000 K (Java/Others)
Total Submission(s): 5995 Accepted Submission(s): 3561
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<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!
思路将PN表打出找规律,因为对称,可以转化为原初始结点(1,1)为终止结点,原终止结点(n, m)转化为初始结点,见程序:
/****************************
*
* acm: hdu-2147
*
* title: kiki's game
*
* time: 2014.5.3
*
*****************************/
//这道题考察博弈论
//可以打出PN表,不过可以直接找到规律,n和m只要有一个为偶数则必胜
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 2000
#define P 1 //先手必败结点 终结点必是必败结点
#define N 0 //先手必胜结点
typedef int Map[MAXSIZE + 1][MAXSIZE + 1];
Map map; //如果放在局部会栈会溢出 (全局变量比局部变量的范围大)
//方法1:PN打表求解
//因为对称,可以转化为原初始结点(1,1)为终止结点,原终止结点(n, m)转化为初始结点
//终止结点值必为p
int main()
{
int n;
int m;
while (~scanf("%d%d", &n, &m) && n!=0 && m!=0)
{
int i;
int j;
for (i = 0; i <= n; i++) //目的 递推求出 初始结点值
{
for (j = 0;j <= m; j++)
{
if(i == 0 || j == 0)
{
map[i][j] = N;
}
else
{
if (map[i-1][j]== N && map[i-1][j-1] == N && map[i][j-1] == N)
{
map[i][j] = P;
}
else
{
map[i][j] = N;
}
}
}
} //end of for
if (map[n][m] == P)
{
printf("What a pity!\n");
}
else
{
printf("Wonderful!\n");
}
/*
//显示NP表
for (i = 1; i <= n; i++)
{
for (j = 1;j <= m; j++)
{
if (map[i][j] == P)
{
printf("P ");
}
else
{
printf("N ");
}
}
printf("\n");
}
*/
}
return 0;
}
PN打表得出的结果提交后,程序内存超限,于是找规律得出,发现 P出现满足条件m%2==1 n%2==1 else 是 N:
/****************************
*
* acm: hdu-2147
*
* title: kiki's game
*
* time: 2014.5.3
*
*****************************/
//这道题考察博弈论
//可以打出PN表,不过可以直接找到规律,n和m只要有一个为偶数则必胜
#include <stdio.h>
#include <stdlib.h>
//方法2:通过PN打表找出规律
//P出现满足条件m%2==1 n%2==1 else 是 N
int main()
{
int n;
int m;
while (~scanf("%d%d", &n, &m) && n!=0 && m!=0)
{
if (n%2==1 && m%2==1)
{
printf("What a pity!\n");
}
else
{
printf("Wonderful!\n");
}
}
return 0;
}