Zoj2686 Cycle Game --- dfs regular search Game

Source: Internet
Author: User

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:
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 shortstrates 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 shoshould 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
 
When I first read this question, I thought it was a water question, no matter how much data it was. After a click, I submitted the question, and it was a gorgeous TLE ~
In fact, this question is regular, but unfortunately I did not see it, sadly ~
In either direction, as long as the number of consecutive non-zero numbers in a certain direction is an odd number, the first hand has a winning strategy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.