Fighting for Triangles

题目连接:

http://codeforces.com/gym/100015/attachments

Description

Andy and Ralph are 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 two 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), 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 that it is Andy’s turn,

where the board has fives edges as shown in the picture below. If Andy draws edge 6, then he will claim the

triangle formed by edge 4, 5, and 6, and continue playing.

Given a board that already has some edges drawn on it, decide the winner of the game assuming that

both Andy and Ralph play optimally. Andy always goes first. Note that if a triangle exists on the board

before the first 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 ! 10, which indicates the number of edges that are 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 are used for clarity and should not be printed. For example, the

correct output for the sample input above would be:

Sample Input

6

1 2 3 4 5 6

5

4 5 6 7 8

0

Sample Output

Andy wins

Ralph wins

Hint

题意

给你一个四层的三角形,一共有18条边

然后A,B两个开始划线,如果当前人划线的时候,正好化成了一个小三角形,他就加一分,并且可以再画

否则就该另外一个人画

然后告诉你一些边已经被画过线了,问你先手是否能够胜利

题解:

直接暴力状压dp就好了

dp[i]表示i这个状态的时候,A能够拿到的最多数

注意trick,就是画线的时候,不仅仅可以占据一个三角形,有可能占据两个三角形哦

代码

#include<bits/stdc++.h>
using namespace std; int dp[1<<21][2];
int Dis[1<<21][2];
int cal(int x)
{
int vis[30];
memset(vis,0,sizeof(vis));
for(int i=1;i<=18;i++)
if((x>>i)&1)
vis[i]=1;
int ans = 0;
for(int i=0;i<6;i++)
if(vis[i*3+1]&&vis[i*3+2]&&vis[i*3+3])
ans++;
if(vis[3]&&vis[5]&&vis[7])ans++;
if(vis[6]&&vis[11]&&vis[13])ans++;
if(vis[9]&&vis[14]&&vis[16])ans++;
return 9-ans;
}
int dfs(int now,int flag)
{
if(Dis[now][flag])return dp[now][flag];
int last = cal(now);
Dis[now][flag]=1;
for(int i=1;i<=18;i++)
{
if(((now>>i)&1)==0)
{
int next = now|(1<<i);
int flag3 = cal(now)-cal(next);
if(flag3>0)
dp[now][flag]=max(dp[now][flag],dfs(next,flag)+flag3);
else
dp[now][flag]=max(dp[now][flag],last-dfs(next,1-flag));
}
}
return dp[now][flag];
} int main()
{
//freopen("1.in","r",stdin);
int n;
while(cin>>n)
{
if(n==0)break;
int now = 0;
for(int i=0;i<n;i++)
{
int x;scanf("%d",&x);
now|=(1<<x);
}
int last = cal(now);
int a = dfs(now,0);
int b = last - a;
//cout<<last<<" "<<a<<" "<<b<<endl;
if(a>b)
cout<<"Andy wins"<<endl;
else if(a==b)
cout<<"Draw"<<endl;
else if(a<b)
cout<<"Ralph wins"<<endl;
}
}

Codeforces Gym 100015F Fighting for Triangles 状压DP的更多相关文章

  1. Codeforces Gym 100015F Fighting for Triangles 状态压缩DP

    F Fighting for Triangles Description Andy and Ralph are playing a two-player game on a triangular bo ...

  2. codeforces gym #101161H - Witcher Potion(状压DP)

    题目链接: http://codeforces.com/gym/101161/attachments 题意: 总共有n瓶药可供选择 每瓶药可以增加$e_i$点体力,和$p_i$点毒性 每分钟消耗1点毒 ...

  3. codeforces 342D Xenia and Dominoes(状压dp+容斥)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud D. Xenia and Dominoes Xenia likes puzzles ...

  4. Codeforces 375C - Circling Round Treasures(状压 dp+最短路转移)

    题面传送门 注意到这题中宝藏 \(+\) 炸弹个数最多只有 \(8\) 个,故考虑状压,设 \(dp[x][y][S]\) 表示当前坐标为 \((x,y)\),有且仅有 \(S\) 当中的物品被包围在 ...

  5. Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)

    Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...

  6. Codeforces 580D Kefa and Dishes(状压DP)

    题目大概说要吃掉n个食物里m个,吃掉各个食物都会得到一定的满意度,有些食物如果在某些食物之后吃还会增加满意度,问怎么吃满意度最高. dp[S][i]表示已经吃掉的食物集合是S且刚吃的是第i个食物的最大 ...

  7. CodeForces 385 D.Bear and Floodlight 状压DP

    枚举灯的所有可能状态(亮或者不亮)(1<<20)最多可能的情况有1048576种 dp[i]表示 i 状态时灯所能照射到的最远距离(i 的二进制中如果第j位为0,则表示第j个灯不亮,否则就 ...

  8. Codeforces 599E Sandy and Nuts(状压DP)

    题目链接 Sandy and Nuts 题意大概就是给出限制条件求出在该限制条件下树的种数. #include <bits/stdc++.h> using namespace std; # ...

  9. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

随机推荐

  1. Yii入门,登录

    验证和授权在页面需要限制访问时用到.验证就是确认某人就是他所声称的那个人.通常涉及到用户名和密码,但也包含其他方式,例如智能卡,指纹等.授权是在验证用户后,查明他是否被允许管理指定的资源.通常判断他是 ...

  2. C++ 虚拟继承

    1.为什么要引入虚拟继承 虚拟继承是多重继承中特有的概念.虚拟基类是为解决多重继承而出现的.如:类D继承自类B1.B2,而类B1.B2都继 承自类A,因此在类D中两次出现类A中的变量和函数.为了节省内 ...

  3. LR录制脚本IE不能打开解决方法

    运行环境:win7 64位 解决方法:1.卸载IE11 2.计算机——属性——高级系统设置——性能里的设置——数据执行保护——选择“为除下列选定程序之外的所有程序和服务启用”——添加IE浏览器和VUG ...

  4. python在linux上的GUI无法弹出界面

    在进行python写GUI程序的时候,使用Tkinter,发现无法执行程序,报错如下: X connection to localhost:10.0 broken(explicit kill or s ...

  5. 封装cookie.js、EventUtil.js、

    最近学习了javascript,封装好的东西看起来舒服,以备需要的时候拉出来,jquery对javascript做了很好的封装!以后会多用jquery多些 var CookieUtil = { get ...

  6. [转] Web前端优化之 图片篇

    原文链接: http://lunax.info/archives/3101.html Web 前端优化最佳实践第六部分面向 图片(Image),这部分目前有 4 条规则.在最近的 Velocity 2 ...

  7. TopFreeTheme精选免费模板【20130703】

    今天我们给大家分享13个最新的主题模板,5款WordPress主题,5款Joomla模板,3款OpenCart主题. BowThemes – BT Folio v1.0 Template for Jo ...

  8. 根据headerView位置改变headerView颜色(collectionView/tableview)

    滑动时,tableview中的headerView 的frame不断改变,collectionView的headerView的center不断改变.sotableview: -(void)setFra ...

  9. flex编译命令相关

    最近碰到几次flex组件集版本问题,mx容器包含s组件,错误百出,会一直提示皮肤文件错误,上网查了一下,只要在工程属性中--->Flex编译器--->附加的编译参数中加入如下命令行即可:- ...

  10. 未能加载文件或程序集“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。

    未能加载文件或程序集“Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed”或它的某一个 ...