POJ 2234 Matches Game(博弈)

 

题目大意

 

桌面上有 m(1m20) 堆火柴,每堆火柴中有若干根火柴(不超过 10000000)。两人轮流玩游戏。每回合,游戏者任意选取一堆火柴,取走若干根火柴(大于 0,不高于该堆火柴的数量),取走最后一根火柴的胜利

给定一个局面,问先手能否胜利

 

做法分析

 

典型的 Nim 博弈问题,直接对每堆火柴的数量取异或和,最后如果大于 0,先手必胜,否则先手必败

 

参考代码

 

POJ 2234 Matches Game(博弈)
 1 #include <iostream>

 2 #include <cstring>

 3 #include <cstdio>

 4 

 5 using namespace std;

 6 

 7 int n, ans;

 8 

 9 int main()

10 {

11     while(scanf("%d", &n)!=EOF)

12     {

13         ans=0;

14         for(int i=0, a; i<n; i++)

15         {

16             scanf("%d", &a);

17             ans^=a;

18         }

19         if(ans) printf("Yes\n");

20         else printf("No\n");

21     }

22     return 0;

23 }
POJ 2234

 

题目链接 & AC通道

 

POJ 2234 Matches Game

 

 

 

你可能感兴趣的:(matches)