hduoj 2147 kiki's game(巴什博弈)

kiki's game

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others)
Total Submission(s): 9890    Accepted Submission(s): 5973


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!

【題目分析】

一切博弈都是找規律:

題目的意思是從棋盤的最右上角到左下角,其中只可以走三個方向, 左邊, 下邊,右邊,不能移動着失敗,問先手是否勝利
根據博弈論的理論,先從左下角開始分析

博弈論:組合博弈
必敗點(P點) :前一個選手(Previous player)將取勝的位置稱爲必敗點。
必勝點(N點) :下一個選手(Next player)將取勝的位置稱爲必勝點。
必敗(必勝)點的屬性:
(1) 所有終結點是必敗點(P點);
(2) 從任何必勝點(N點)操作,至少有一種方法可以進入必敗點(P點);
(3)無論如何操作, 從必敗點(P點)都只能進入必勝點(N點).
 由上面的屬性得到該題的算法:

步驟1:將所有終結位置標記爲必敗點(P點);
步驟2: 將所有一步操作能進入必敗點(P點)的位置標記爲必勝點(N點)
步驟3:如果從某個點開始的所有一步操作都只能進入必勝點(N點) ,則將該點標記爲必敗點(P點) ;
步驟4: 如果在步驟3未能找到新的必敗(P點),則算法終止;否則,返回到步驟2。
   由上面的算法計算一個例子:
我們可以把問題轉換成從(1,1)走到(n,m) (方便等下得出結論)
但n=8,m=9的情況
 NNNNNNNNN
 PNPNPNPNP
 NNNNNNNNN
 PNPNPNPNP
 NNNNNNNNN
 PNPNPNPNP
 NNNNNNNNN
 PNPNPNPNP
初始點(1,1)爲N所以輸出Wonderful!
從這裏例子就可以很清楚得看出當n和m都爲奇數時,初始點(1,1)纔會是P。
因此該題只需判斷n,m是否同時爲奇數即可。



AC代碼:
#include<stdio.h>
int main()
{
  int n,m;
  while(scanf("%d%d",&n,&m)!=EOF&&n!=0&&m!=0)
  {
     if(m&1&&n&1)
          printf("What a pity!\n");
     else
          printf("Wonderful!\n");//行數或者列數爲偶數就贏 
  }
  return 0;
}
Sample Output
What a pity! Wonderful! Wonderful!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章