HDU - 4994 Revenge of Nim (取石子游戏)

Problem Description
Nim is a mathematical game of strategy in which two players take turns removing objects from distinct heaps. On each turn, a player must remove at least one object, and may remove any number of objects provided they all come from the same heap.
---Wikipedia

Today, Nim takes revenge on you. The rule of the game has changed a little: the player must remove the objects from the current head(first) heap. Only the current head heap is empty can the player start to remove from the new head heap. As usual, the player who takes the last object wins.

 
Input
The first line contains a single integer T, indicating the number of test cases.

Each test case begins with an integer N, indicating the number of heaps. Then N integer Ai follows, indicating the number of each heap successively, and the player must take objects in this order, from the first to the last.

[Technical Specification]
1. 1 <= T <= 100
2. 1 <= N <= 1 000
3. 1 <= Ai <= 1 000 000 000

 
Output
For each test case, output “Yes” if the first player can always win, otherwise “No”.
Sample Input

Sample Output
Yes
No

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4994

************************************************

题意:有n堆石子,每堆有ai个,每次按照堆的先后顺序至少取走一个石子,最后取完石子的人赢,如果第一个取石子的人赢则输出Yes,否则,输出No。

分析:一开始老是从大于1的上面去判断,后来又琢磨琢磨,才发现只需要对第一个大于1的堆之前等于1的个数来判断就可以了。

如果每堆石子的数目都大于1,则first在取前n-1堆石子时,每次都留下一个石子,在取最后一堆时,一次取完,则first必赢;

但是如果有些堆的石子数不大于1时,当每出现一个大于1的石子堆时,对于取该堆石子的人,他有两种取法,一是全部取走,二是取走ai-1个(即留下1个),而这两种方式在不同的情况中可以使用其中一种使得自己更容易赢。

因此只要判断出从前往后的前n-1堆中,谁先取得第一堆数目大于1的石子堆的主动权,谁就是赢者。

所以只需要求出第一堆数目大于1的石子堆前有多少个1就可以判断了,如果有偶数个1, 则Yes,如果有奇数个1,则No。

AC代码:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <map>
#include <vector>
using namespace std; #define N 1200
#define INF 0x3f3f3f3f int a[N]; int main()
{
int T,n,i; scanf("%d", &T); while(T--)
{
scanf("%d", &n); int ans=;
for(i=;i<n;i++)
scanf("%d", &a[i]); for(i=;i<n-;i++)///前n-1堆中第一个大于1的堆前的1的个数
if(a[i]==)
ans++;
else///不为1时要break
break; if(ans%==)
printf("Yes\n");
else
printf("No\n");
}
return ;
}
上一篇:HDU 2516 取石子游戏 (博弈论)


下一篇:Linux - 变量