ZOJ 2686 Cycle Game(博弈 找规律)

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1686

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

Source: Asia 2003, Seoul (South Korea)

题意:

有N个点,形成一个环,相邻点之间有权值。从0号点开始,可以选择往两边移动,前提是权值为正。

移动之后,将权值减少一个任意的正数。最后无法移动者败。

PS:

此题需要找规律,直接搜会T;

两个方向,如果某个方向连续的非0个数为奇数的话,先手必胜。

先手的策略是,将权值降为0,后手不能返回了,只能朝一个方向去,如果后手继续将权值降为0,那么先手依旧是继续,因为个数为奇数,先手必胜。如果后手不把权值降为0,那么先手返回一步,将权值降为0,出现两边都为0,必胜。

代码如下:

#include <cstdio>
#include <cstring>
int main()
{
    int t;
    int n;
    int a[26];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d",&a[i]);
        }
        int cont1 = 0, cont2 = 0;
        for(int i = 0; i < n; i++)
        {
            if(a[i])
            {
                cont1++;
            }
            else
                break;
        }
        for(int i = n-1; i >= 0; i--)
        {
            if(a[i])
            {
                cont2++;
            }
            else
                break;
        }
        if(cont1&1 || cont2&1)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
时间: 2024-12-18 00:30:12

ZOJ 2686 Cycle Game(博弈 找规律)的相关文章

HDU 1564 Play a game (博弈&amp;&amp;找规律)

Play a game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1430    Accepted Submission(s): 1168 Problem Description New Year is Coming! ailyanlu is very happy today! and he is playing a chessbo

HDU 1517 A Multiplication Game (博弈&amp;&amp;找规律)

A Multiplication Game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 3691    Accepted Submission(s): 2097 Problem Description Stan and Ollie play the game of multiplication by multiplying an in

HDU 2147-kiki&#39;s game(博弈/找规律)

kiki's game Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 40000/10000 K (Java/Others) Total Submission(s): 9174    Accepted Submission(s): 5485 Problem Description Recently kiki has nothing to do. While she is bored, an idea appears in his

51nod_1831: 小C的游戏(Bash博弈 找规律)

题目链接 此类博弈不需要考虑sg函数,只需要确定必胜态和必败态,解题思路一般为打败先打表找规律,而后找规律给出统一的公式.打表方式:给定初始条件(此题中为ok[0]=ok[1]=0),然后从低到高枚举某一状态的所有次态,若有存在必败次态,则当前状态为必胜态,否则当前状态必败. 题意:对单独一堆石子,支持两种操作:1.石子数-1:2.石子数变为原来石子数的某一因数.取走走后一堆或无法操作(面对n==0,坑啊..)者为负. 先打表找下规律 1 #include<bits/stdc++.h> 2 u

Acdream 1416 Crazy Nim(简单博弈找规律)

传送门 Crazy Nim Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description Alice and Bob like to play crazy nim. The game proceeds as follows. There are several stones arranged in

zoj 3629 Treasure Hunt IV(找规律)

Alice is exploring the wonderland, suddenly she fell into a hole, when she woke up, she found there are b - a + 1 treasures labled a fromb in front of her.Alice was very excited but unfortunately not all of the treasures are real, some are fake.Now w

HDU 1079 Calendar Game(博弈找规律)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1079 题目大意:给你一个日期(包含年月日),这里我表示成year,month,day,两人轮流操作,每次操作可以将month+1但是,如果下月没有对应的day则只能对day+1(超过该月日数就进入下月一日),或者就day+1.谁最后到达2001.11.4这个日期就是胜者,问先手的人是否能获胜. 解题思路:这个就用上面的P/N分析,一个个月份日期对应的标记上P或N(很快会发现规律只用找每月特定几天),

组队赛#1 解题总结 ZOJ 3798 Abs Problem (找规律+打表)

Abs Problem Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Alice and Bob is playing a game, and this time the game is all about the absolute value! Alice has N different positive integers, and each number is not greater than N.

ZOJ 3829 Known Notation --贪心+找规律

题意:给出一个字符串,有两种操作: 1.插入一个数字  2.交换两个字符   问最少多少步可以把该字符串变为一个后缀表达式(操作符只有*). 解法:仔细观察,发现如果数字够的话根本不用插入,数字够的最低标准为'*'的个数+1,因为最优是 '12*3*..' 这种形式,所以先判断够不够,不够就补,然后从左往右扫一遍,如果某个时刻Star+1>Num,那么从开始到这一段是不合法的,要把那个'*'与后面的一个数字交换,此时Star--,Num++.然后步数++.这样得出的结果就是最后的最小步数. 脑子