F Fighting for Triangles     

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

题意:给一个图, 是一个正三角形,在三角形内部(包括边缘)有18个点,每次你可以去相邻的点画一条线段,假如这条线段可以和相邻的点构成一个新的三角形,那么价值+1

    现在有两个人玩这个比赛,A先手,假如A不能获取价值 才轮到B 知道所有可能的线段全部画完,问你A,B谁获取的价值更大

  开始给你n个点,表示这n个点间有线段已经被画完

题解:电数n<=18我们设定 x为当前被画掉的状态,且f先手

那么 记忆花搜索dp[x][f] 表示就是当前x情况下 f先手  a 取得的价值是多少

  爆搜记忆就好

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std ;
typedef long long ll;
const int N = <<;
int dp[N][],v[];
int cal(int x) {
memset(v,,sizeof(v));
int ret = ;
for(int i = ; i >= ; i--) if(x & (<<i)) v[i + ] = ;
for(int i = ; i <= ; i += ) {
if(v[i] && v[i+] && v[i+]) ret++;
}
if(v[] && v[] && v[]) ret++;
if(v[] && v[] && v[]) ret++;
if(v[] && v[] && v[]) ret++;
return ret;
}
int dfs(int n,int f) {
if(dp[n][f] != -) return dp[n][f];
int last = - cal(n);
dp[n][f] = ;
for(int i = ; i < ; i++) {
if(((<<i) & n) != ) continue;
int nex = n|(<<i);
int g = cal(nex) - cal(n);
if(g) dp[n][f] = max(dp[n][f],dfs(nex,f) + g);
else dp[n][f] = max(dp[n][f], last - dfs(nex,-f));//
}
return dp[n][f];
}
int main() {
int n, x;
memset(dp,-,sizeof(dp));
while(scanf("%d",&n)!=EOF) {
if(n == ) break;
int f = ;
for(int i = ; i <= n; i++) scanf("%d",&x), f |= (<<(x-));
int last = - cal(f);
// for(int i = 1; i <= 18; i++)if(v[i]) printf("1");else cout<<0;
int a = dfs(f,);
int b = last - a;
if(a > b) printf("Andy wins\n");
else if(a == b) printf("Draw\n");
else printf("Ralph wins\n");
}
return ;
}

代码

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

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

    Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...

  2. Codeforces C. A Simple Task(状态压缩dp)

    题目描述:  A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  3. Codeforces 903F Clear The Matrix(状态压缩DP)

    题目链接 Clear The Matrix 题意 给定一个$4 * n$的矩形,里面的元素为$'.'$或$'*'$.现在有$4$种正方形可以覆盖掉$'*'$,正方形的边长分别为$1,2,3,4$. 求 ...

  4. Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest

    Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...

  5. hoj2662 状态压缩dp

    Pieces Assignment My Tags   (Edit)   Source : zhouguyue   Time limit : 1 sec   Memory limit : 64 M S ...

  6. POJ 3254 Corn Fields(状态压缩DP)

    Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4739   Accepted: 2506 Descr ...

  7. [知识点]状态压缩DP

    // 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...

  8. HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP

    题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...

  9. DP大作战—状态压缩dp

    题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...

随机推荐

  1. Python关于super()函数的理解

    看下面的例子: class A: def __init__(self, name): self.name = name def bb(self): print('没事就爱瞎BB') class B(A ...

  2. SQLServer 表连接时使用top 1 去除重复数据

    left join SM_SOLine soline on soline.SO=so.ID and soline.DocLineNo=(select MAX(DocLineNo) from SM_SO ...

  3. Css 分类 属性 选择器

    Css 层叠样式表 美化页面的小工具 分类: 内联 (行内)在标签内部以属性的形式呈现,属性名style 内嵌 head标签内以标签形式呈现,标签名style 外部 head标签内 加link标签 引 ...

  4. Android拍照,录制视频,相机简单功能实现

    1.效果图,功能没有录制出来. 基本实现了拍照,录制视频,前后摄像头切换的功能,可以转屏,聚焦. 代码在最下面,可以看代码,运行apk看实际效果. 2.权限不能忘 <uses-permissio ...

  5. 使用一行代码解决IE浏览器兼容问题

    在网站开发中不免因为各种兼容问题苦恼,针对兼容问题,其实IE给出了解决方案Google也给出了解决方案 百度也应用了这种方案去解决IE的兼容问题 百度源代码如下 <!Doctype html&g ...

  6. 工厂模式-CaffeNet训练

    参考链接:http://blog.csdn.net/lingerlanlan/article/details/32329761 RNN神经网络:http://nbviewer.ipython.org/ ...

  7. 三维重建:Kinect几何映射-SDK景深数据处理

    此文大量使用XML,非C类的代码,看看图即可. 原文链接:Kinect for Windows SDK开发入门(五):景深数据处理 3. 对物体进行测量 像上篇文章中对深度值测量原理进行讨论的那样,像 ...

  8. adb使用实践

    目录 1. adb 端口占用 2. 查看包名和MainAcitivity =============================================================== ...

  9. C#模拟按键

    try { System.Threading.Thread.Sleep(); ; i < ; i++) { SendKeys.SendWait("{ENTER}"); Sen ...

  10. Java导出Excel文件的两种方法

    将数据以Excel表格的形式导出:首先下载poi的jar包,导入项目中,或者使用maven仓库管理,在pom文件添加:<dependency>    <groupId>org. ...