博弈——Cycle Game(找规律)
题目链接:
ZOJ - 2686
Here is a game played on a cycle by two players. The rule of this game is as follows: At first, a cycle is given and each edge is assigned a non-negative integer. Among those integers, at least one is zero. Further a coin is put on a vertex of the cycle. From this vertex, the game starts and proceeds with two players’ alternating moves with the following series of choices:
Choose an edge incident with the vertex having the coin,
Decrease the value of this edge to any non-negative integer strictly,
Move the coin to the adjacent vertex along this edge.
The game ends when a player on his turn cannot move because the value of each edge incident with the vertex having the coin is equal to zero. Then, that player is the loser.
Figure 1 illustrates an actual game. In this game, Alice is the first player and Bob is the second player. In the starting position in Figure 1 (a), Alice cannot but choose the right edge of the vertex having the coin. Alice then decreases its value from 2 to 0, and moves the coin along this edge, which makes (a) into (b). Next, Bob cannot but choose the down edge of the vertex having the coin; he then decreases its value from 5 to 1, which makes (b) into ©. In Figure 1 ©, Alice chooses the up edge of the vertex having the coin and decreases its value from 1 to 0, which makes © into (d). Finally, in Figure 1 (d), Bob has no move since each edge incident with the vertex having the coin is assigned to zero. Then, Alice wins this game.
Figure 1: An example of cycle game (A coin is put on the black vertex)
In fact, whenever the game starts as shown in Figure 1 (a), the first player can always win for any second player’s move. In other words, in the starting position in Figure 1 (a), the first player has a winning strategy. In this problem, you should determine whether or not the first player has a winning strategy from a given starting position.
Input
The input consists of T test cases. The number of test cases (T) is given on the first line of the input file. Each test case starts with a line containing an integer N (3 <= N <= 20), where N is the number of vertices in a cycle. On the next line, there are the N non-negative integers assigned to the edges of the cycle. The N integers are given in clockwise order starting from the vertex having the coin and they are separated by a single space. Note that at least one integer value among the N integers must be zero and that the value of no integer can be larger than 30.
Output
Print exactly one line for each test case. The line is to contain “YES” if the first player has a winning strategy from the starting position. Otherwise, the line is to contain “NO”. The following shows sample input and output for two test cases.
Sample Input
2
4
2 5 3 0
3
0 0 0
Sample Output
YES
NO
题目大意
有一个 n 边形,每条边上都有一个权值,硬币初始位置在第一个节点上,玩家轮流将硬币移动到相邻节点上,但移动时要注意该边的权值必须为正数才能移动,移动之后,将边的权值减去一个数,不能移动的玩家就算输了。
分析
刚开始游戏时,先手有两个方向可以选择,顺时针和逆时针,当其中一个方向存在连续的非 0 数,并且非 0 数的个数为奇数时,先手必胜。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n,f[30];
cin>>t;
while(t--){
cin>>n;
memset(f,0,sizeof(f));
for(int i=0;i<n;i++){
cin>>f[i];
}
int cnt1=0,cnt2=0;
for(int i=0;i<n;i++){ // 顺时针
if(f[i]==0){
break;
}
else{
cnt1++;
}
}
for(int i=n-1;i>=0;i--){ // 逆时针
if(f[i]==0){
break;
}
else{
cnt2++;
}
}
if(cnt1&1||cnt2&1){ // 某个方向存在连续的非0数,并且其个数为奇数
cout<<"YES"<<endl;
}
else{
cout<<"NO"<<endl;
}
}
return 0;
}