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);elsedp[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;}
}

转载于:https://www.cnblogs.com/qscqesze/p/5136004.html

Codeforces Gym 100015F Fighting for Triangles 状压DP相关推荐

  1. Gym - 101853E E. Maximum Sum (状压DP)

    原题地址:https://codeforces.com/gym/101853/problem/E 题意:给出一个n*n的矩阵,矩阵中每一个格子都有一个权值,选择一个格子就能拿到当前的权值,但是同时就不 ...

  2. Codeforces 1326F Wise Men (容斥原理、状压 DP、划分数)

    题目链接 F1: https://codeforces.com/contest/1326/problem/F1 F2: https://codeforces.com/contest/1326/prob ...

  3. Codeforces 1326F Wise Men (容斥原理、状压 DP、子集和变换、划分数)

    题目链接 F1: https://codeforces.com/contest/1326/problem/F1 F2: https://codeforces.com/contest/1326/prob ...

  4. Codeforces 678E:Another Sith Tournament 状压DP

    odd-even number 题目链接: http://codeforces.com/problemset/problem/678/E 题意: 有n个人打擂台赛,每两个人间都有相对的胜率,主角可以操 ...

  5. CodeForces - 1550E Stringforces(二分+状压dp)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的字符串,只包含前 kkk 个小写字母以及通配符 ???,现在可以将通配符替换成任意的前 kkk 个字母中的一个.设 f[i]f[i]f[i] 为 ...

  6. Codeforces Gym 100676G Training Camp 状压dp

    http://codeforces.com/gym/100676 题目大意是告诉你要修n门课,每门课有一个权值w[i], 在第k天修该课程讲获得k*w[i]的学习点数,给出了课程与先修课程的关系,要修 ...

  7. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

  8. Codeforces Beta Round #8 C. Looking for Order 状压dp

    题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...

  9. codeforces 8C. Looking for Order 状压dp

    题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动. 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程. 直接状压dp就好了. #include < ...

  10. 【Gym - 101915D】Largest Group(二分图最大团,状压dp)

    题干: 大黑山上有小小民和小小涛两种物种,山东人小李想要研究这两种物种的关系 奇怪的是大黑山上有相同数量的小小民和小小涛.小李数了数一共有 P 个,小李分别给P个小小民和小小涛编号 1 - P 号,已 ...

最新文章

  1. 代替Mask R-CNN,BlendMask欲做实例预测任务的新基准?
  2. 第十五届智能车竞赛技术报告-成电金秋-AI电磁
  3. Opencv 实现图像的离散傅里叶变换(DFT)、卷积运算(相关滤波)
  4. asp.net使用mysql教程_在C#程序中使用MYSQL数据库
  5. STL标准容器类简介
  6. SAP生产订单预留(下)
  7. sql长整型_SQL性能优化,太太太太太太太有用了!
  8. 数据库设计方法、规范与技巧
  9. WinForm 图片变灰方法
  10. [深入学习C#]匿名函数、委托和Lambda表达式
  11. Java中的volatile的作用和synchronized作用
  12. Log4j框架配置文件log4j.properties配置使用详解
  13. Qt的环境与工具、信号与槽
  14. java pfx 证书_java 证书 .cer 和 .pfx
  15. Linux系统卡死,只有鼠标能动,解决办法
  16. python这个怎么读-python怎么读(python怎么读中文)
  17. 新手小白怎样玩转短视频抖音吸粉公众号小说项目
  18. android仿微信发状态图片上传
  19. C语言实现二叉排序树
  20. 如何删除日期中的不必要字段

热门文章

  1. CAT的Client端初始化
  2. python基础教程_python基础教程百度网盘
  3. 20172302 《Java软件结构与数据结构》第五周学习总结
  4. 百度云智学院云计算在线认证考试课程纲要与重点,考前必看!
  5. springcloud+eureka简单的邮件监控
  6. php faker,Laravel的Faker的使用
  7. 开放阿里云指定端口及设置本地代理访问自己的阿里云服务器
  8. 【综述】(MIT博士)林达华老师-quot;概率模型与计算机视觉”
  9. 2020 年 Python 知识清单(数据分析)
  10. 用代理服务器加速爬虫速率