Codeforces Gym 100015F Fighting for triangles state compression DP

Source: Internet
Author: User

     F Fighting for triangles  

Description

Andy and Ralph is playing a two-player game on a triangular board that looks like the following:

1 2
3
4 5 7 8
6 9
10 11 13 14 16 17
12 15 18

At each turn, a player must choose the adjacent vertices and draw a line segment that connects them.
If the newly drawn edge results in a triangle on the board (only the smallest ones count) and then the player
Claims the triangle and draws another edge. Otherwise, the turn ends and the other player plays. The
Objective of the game is to claim as many triangles as possible. For example, assume the It is Andy's turn,
Where the board has the VES edges as shown and the picture below. If Andy draws Edge 6, then he'll claim the
Triangle formed by Edge 4, 5, and 6, and continue playing.

Given a board that already have some edges drawn on it, decide the winner of the game assuming that
Both Andy and Ralph play optimally. Andy always goes rst. Note If a triangle exists on the board
Before the RST move, neither player claims it.

Input

The input consists of multiple test cases. Each test case begins with a line containing an integer n,5!
N! Which indicates the number of edges that is already present on the board before the game begins.
The next line contains N integers, indicating the indices of these edges. The input terminates with a line
With N = 0. For example:

Output

For each test case, print out a single line that contains the result of the game. If Andy wins, then print out
"Andy wins". If Ralph wins, then print out "Ralph wins". If Both players get the same number of triangles,
Then print out "Draw". Quotation marks is used for clarity and should isn't be printed. For example, the
Correct output for the sample input above would is:

Sample Input

6

1 2 3 4 5 6

5

4 5 6) 7 8

0

Sample Output

Andy wins

Ralph wins

Test Instructions : to a graph, is a positive triangle, inside the triangle (including the edge) has 18 points, each time you can go to the adjacent points to draw a line segment, if the line can be a new triangle with the adjacent points, then the value of +1

Now there are two people playing this game, a-initiator, if a can not get the value of the turn to B know all the possible line segments are finished, ask you A, a, a, who gets greater value

Start to give you n points, indicating that there is a line between the n points has been painted

n<=18: We set X to the state we are currently drawing, and F-Initiator

Then the memory flower search Dp[x][f] means that is the current X case F Initiator A is the value of how much

Just pop the memory.

#include <iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespacestd; typedefLong Longll;Const intN =1<< +;intdp[n][2],v[ +];intCalintx) {memset (V,0,sizeof(v)); intRET =0;  for(inti = -; I >=0; i--)if(X & (1<<i)) V[i +1] =1;  for(inti =1; I <= -; i + =3) {        if(V[i] && v[i+1] && v[i+2]) ret++; }    if(v[3] && v[5] && v[7]) ret++; if(v[6] && v[ One] && v[ -]) ret++; if(v[9] && v[ -] && v[ -]) ret++; returnret;}intDfsintNintf) {if(Dp[n][f]! =-1)returnDp[n][f]; intLast =9-cal (n); DP[N][F]=0;  for(inti =0; I < -; i++) {        if(((1<<i) & N)! =0)Continue; intNEX = n| (1<<i); intg = cal (NEX)-cal (n); if(g) Dp[n][f] = max (Dp[n][f],dfs (nex,f) +g); ElseDP[N][F] = max (dp[n][f], Last-dfs (NEX,1-f));//    }    returndp[n][f];}intMain () {intN, x; Memset (DP,-1,sizeof(DP));  while(SCANF ("%d", &n)! =EOF) {        if(n = =0) Break; intf =0;  for(inti =1; I <= N; i++) scanf ("%d", &x), F |= (1<< (x1)); intLast =9-cal (f); //for (int i = 1; i <=; i++) if (V[i]) printf ("1"); else cout<<0;        intA = DFS (f,0); intb = Last-A; if(A > B) printf ("Andy wins\n"); Else if(A = = b) printf ("draw\n"); Elseprintf"Ralph wins\n"); }    return 0;}
Code

Codeforces Gym 100015F Fighting for triangles state compression DP

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.