zoj2686 Cycle Game---dfs 找规律 博弈

Cycle Game

Time Limit: 5 Seconds      Memory Limit: 32768 KB

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:

  1. Choose an edge incident with the vertex having the coin,
  2. Decrease the value of this edge to any non-negative integer strictly,
  3. 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 (c). In Figure 1 (c), Alice chooses the up edge of the vertex having the coin and decreases its value from 1 to 0, which makes (c) 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
 
刚看这题的时候就觉得是水题,也不管数据,咔咔的敲完,一提交,5001s华丽的TLE~
其实这题是有规律的,可惜我没看出来,悲哀~
朝两个方向,只要某个方向的连续的非0个数为奇数,先手就有必胜策略。
参考:http://blog.csdn.net/acm_cxlove/article/details/7850050
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#define C    240
#define TIME 10
#define inf 1<<25
#define LL long long
using namespace std;
int n,a[30];
int slove(int m)
{
    //顺时针找非0个数,如果是奇数,直接返回必胜
    int cnt=0;
    for(int i=m;cnt<n;i=(i+1)%n)
        if(a[i]==0)
            break;
        else
            cnt++;
    if(cnt&1)
        return 1;
    //逆时针找非0个数,如果是奇数,直接返回必胜
    /*cnt=0;
    for(int i=(m-1+n)%n;cnt<n;i=(i-1+n)%n)
        if(a[i]==0)
            break;
        else
            cnt++;
    if(cnt&1)
        return 1;*/
    for(int i=-1;i<=1;i+=2)
    {
        int k;
        if(i==1)
            k=m;
        else
            k=(m-1+n)%n;
        for(int j=1;j<=a[k];j++)
        {
            a[k]-=j;
            if(!slove((m+i+n)%n))
            {
                a[k]+=j;
                return 1;
            }
            a[k]+=j;
        }
    }
    return 0;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        scanf("%d",&a[i]);
        puts(slove(0)?"YES":"NO");
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值