Codeforces Gym 100015F Fighting for Triangles 状态压缩DP
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的更多相关文章
- Codeforces Gym 100015F Fighting for Triangles 状压DP
Fighting for Triangles 题目连接: http://codeforces.com/gym/100015/attachments Description Andy and Ralph ...
- Codeforces C. A Simple Task(状态压缩dp)
题目描述: A Simple Task time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 903F Clear The Matrix(状态压缩DP)
题目链接 Clear The Matrix 题意 给定一个$4 * n$的矩形,里面的元素为$'.'$或$'*'$.现在有$4$种正方形可以覆盖掉$'*'$,正方形的边长分别为$1,2,3,4$. 求 ...
- Codeforces 4538 (状态压缩dp)Little Pony and Harmony Chest
Little Pony and Harmony Chest 经典状态压缩dp #include <cstdio> #include <cstring> #include < ...
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
随机推荐
- Ajax请求成功但是一直进入error的原因
1.在1.3版本的jQuery以后,严格要求了json格式,如果返回的值不是json格式,他就会执行error函数. 所以如果想让他走success函数的话,还是在后台把数据格式化成json格式吧. ...
- LeetCode Weekly Contest 26
写的有点晚了. 我每次都是先看一下这里http://bookshadow.com/leetcode/的思路,然后再开始写我自己的. 1. 521. Longest Uncommon Subsequen ...
- 【Oracle】回收站
☆回收站概念 oracle从10g开始,引入回收站(Recycle Bin)概念.回收站的全称叫:Tablespace Recycle Bin.回收站是一个逻辑区域,oracle并没有为它分配物理空间 ...
- 01--vim常用快捷键
Linux中vim编辑器的功能非常强大,许多常用快捷键用起来非常方便,这里将我学vim入门时学的一些常用的快捷键分享给大家一下,希望可以帮助你们. 这个是我将鸟哥书上的进行了一下整理的,希望不要涉 ...
- What's Dead & Exploded in Swift's exception stack?
The Swift compiler marks function arguments for a number of reasons, mostly related to internal opti ...
- 熟悉VS2017 和Github 第二次作业
GIT地址 https://github.com/Astone1213 GIT用户名 Astone1213 学号后五位 62114 博客地址 https://www.cnblogs.com/AsL ...
- K8s的工作原理
title: Kubernetes之初探 subtitle: K8s的工作原理 date: 2018-09-18 18:26:37 --- K8s概述 我清晰地记得曾经读到过的一篇博文,上面是这样写的 ...
- Scala 技术笔记之 可变长参数
转自 http://www.cnblogs.com/rollenholt/p/4112833.html Scala 允许你指明函数的最后一个参数可以是重复的.这可以允许客户向函数传入可变长度参数列表. ...
- SLAB
slab:由于内核会有许多小对象,这些对象构造销毁十分频繁,比如i-node,dentry,这些对象如果每次构建的时候就向内存要一个页(4kb),而其实只有几个字节,这样就会非常浪费,为了解决这个问题 ...
- git入坑随笔
一直以来,都喜欢用git做版本管理,主要涉及平台是coding, github以及公司自己搭建的gitlab. 因为一直以来都是自己一个人维护前端的项目,所以基本上很少有冲突的情况.(手动微笑 :) ...