C - kiki’s game
题目
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?
题意:
kiki和ZZ玩游戏,有一个n*m的矩阵,在右上角(1,m)的位置放置着一枚硬币,两个人轮流移动硬币,硬币可以往左边,下边以及左下方移动,每次移动一个空位,最终不能移动硬币的人输掉比赛,如果kiki和ZZ都采用最优的策略,那么请问谁将会赢得比赛?
类型:
巴什博弈
分析:
可以用1表示必胜点,0表示必败点,那么可以写出这样的矩阵(n=4,m=4):
1 1 1 1
0 1 0 1
1 1 1 1
0 1 0 1
由此可得 构造矩阵map[2][2],并且map[2][2]={1,1,1,0}(因为当n=1,m=1时,局势为必败状态,那么map[1][1]=0,其余的2*2的矩阵中的局势均为必胜局势,所以全部为1)。
代码
#include<stdio.h>
int map[2][2] = {1,1,1,0};
int main()
{
int n, m;
while(scanf("%d%d", &n, &m)&&n&&m)
{
n = n % 2;
m = m % 2;
if( map[n][m] == 1) printf("Wonderful!\n");
else
printf("What a pity!\n");
}
return 0;
}