poj 1085 Triangle War (状压+记忆化搜索)
|
Triangle War
Description
Triangle War is a two-player game played on the following triangular grid:
![]() Two players, A and B, take turns filling in any dotted line connecting two dots, with A starting first. Once a line is filled, it cannot be filled again. If the line filled by a player completes one or more triangles, she owns the completed triangles and she is awarded another turn (i.e. the opponent skips a turn). The game ends after all dotted lines are filled in, and the player with the most triangles wins the game. The difference in the number of triangles owned by the two players is not important. For example, if A fills in the line between 2 and 5 in the partial game on the left below: Input
You will be given a number of games in the input. The first line of input is a positive integer indicating the number of games to follow. Each game starts with an integer 6 <= m <= 18 indicating the number of moves that have been made in the game. The next
m lines indicate the moves made by the two players in order, each of the form i j (with i < j) indicating that the line between i and j is filled in that move. You may assume that all given moves are legal. Output
For each game, print the game number and the result on one line as shown below. If A wins, print the sentence "A wins." If B wins, print "B wins."
Sample Input 4 Sample Output Game 1: B wins. Source |
题意:
两个人玩游戏,依次在三角形上放边,假设能构成三角形。则奖励继续该此人放,问最后得到的三角形多。
思路:
给边编号,记忆化搜索即可。做过好多这样的题。就不多写思路了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 1005
#define MAXN 50005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;
int n,m,ans,cnt,tot,flag;
bool vis[10];
int dp[300000],mp[15][15],sc[5];
int tri[9][3]=
{
0,1,2,3,4,7,2,4,5,5,6,8,9,10,15,
7,10,11,11,12,16,8,12,13,13,14,17
};
int cal(int s)
{
int i,j,t=0;
for(j=0; j<9; j++)
{
if((s&(1<<tri[j][0]))&&(s&(1<<tri[j][1]))&&(s&(1<<tri[j][2]))) t++;
}
return t;
}
int dfs(int state,int score)
{
if(dp[state]!=-1) return dp[state];
int i,j,t,tst,num,best=0,tmp;
num=9-score;
for(i=0; i<=17; i++)
{
if(state&(1<<i)) continue ;
tst=state|(1<<i);
t=cal(tst);
if(t>num)
{
tmp=t-num+dfs(tst,score-(t-num));
best=max(best,tmp);
}
else
{
tmp=score-dfs(tst,score);
best=max(best,tmp);
}
}
dp[state]=best;
return best;
}
int main()
{
int i,j,t,test=0;
mp[1][2]=0;mp[1][3]=1;mp[2][3]=2;mp[2][4]=3;mp[2][5]=4;mp[3][5]=5;
mp[3][6]=6;mp[4][5]=7;mp[5][6]=8;mp[4][7]=9;mp[4][8]=10;mp[5][8]=11;
mp[5][9]=12;mp[6][9]=13;mp[6][10]=14;mp[7][8]=15;mp[8][9]=16;mp[9][10]=17;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
tot=0;
int x,y,z,turn=0,num=0;
sc[0]=sc[1]=0;
memset(vis,0,sizeof(vis));
for(i=1; i<=n; i++)
{
scanf("%d%d",&x,&y);
z=mp[x][y];
tot|=(1<<z);
flag=0;
for(j=0; j<9; j++)
{
if(vis[j]) continue ;
if((tot&(1<<tri[j][0]))&&(tot&(1<<tri[j][1]))&&(tot&(1<<tri[j][2])))
{
vis[j]=1;
num++;
flag=1;
sc[turn]++;
}
}
if(!flag) turn^=1;
}
memset(dp,-1,sizeof(dp));
z=dfs(tot,9-num);
sc[turn]+=z;
sc[turn^1]+=(9-num-z);
if(sc[0]>sc[1]) printf("Game %d: A wins.\n",++test);
else printf("Game %d: B wins.\n",++test);
}
return 0;
}
poj 1085 Triangle War (状压+记忆化搜索)的更多相关文章
- Luogu P2831 愤怒的小鸟(状压+记忆化搜索)
P2831 愤怒的小鸟 题意 题目描述 Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于\((0,0)\)处,每次Kiana可以用它向第一象限发射 ...
- poj 1085 Triangle War 博弈论+记忆化搜索
思路:总共有18条边,9个三角形. 极大极小化搜索+剪枝比较慢,所以用记忆化搜索!! 用state存放当前的加边后的状态,并判断是否构成三角形,找出最优解. 代码如下: #include<ios ...
- POJ 1191 棋盘分割 【DFS记忆化搜索经典】
题目传送门:http://poj.org/problem?id=1191 棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submission ...
- POJ 1579 Function Run Fun 【记忆化搜索入门】
题目传送门:http://poj.org/problem?id=1579 Function Run Fun Time Limit: 1000MS Memory Limit: 10000K Tota ...
- (中等) POJ 1054 The Troublesome Frog,记忆化搜索。
Description In Korea, the naughtiness of the cheonggaeguri, a small frog, is legendary. This is a we ...
- POJ 3249 Test for Job (记忆化搜索)
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11830 Accepted: 2814 Des ...
- HDU 2517 / POJ 1191 棋盘分割 区间DP / 记忆化搜索
题目链接: 黑书 P116 HDU 2157 棋盘分割 POJ 1191 棋盘分割 分析: 枚举所有可能的切割方法. 但如果用递归的方法要加上记忆搜索, 不能会超时... 代码: #include& ...
- poj 1088 滑雪(区间dp+记忆化搜索)
题目链接:http://poj.org/problem?id=1088 思路分析: 1>状态定义:状态dp[i][j]表示在位置map[i][j]可以滑雪的最长区域长度: 2>状态转移方程 ...
- POJ 1088: 滑雪(经典 DP+记忆化搜索)
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 74996 Accepted: 27818 Description ...
随机推荐
- 爬虫之robots.txt
robots是网站跟爬虫间的协议,用简单直接的txt格式文本方式告诉对应的爬虫被允许的权限,也就是说robots.txt是搜索引擎中访问网站的时候要查看的第一个文件. 当一个搜索蜘蛛访问一个站点时,它 ...
- 备忘 ubuntu ip 及 dns 的坑
以前都用 ubuntu 16.04 现在用 18.04 遇到几个恶心的事,现在解决了,记录下来. 1. 设置 DNS , DNS 设置老是不对,最后发现问题老版本 ubuntu 17.10以下 ...
- 【solr】Solr5.5.4+Zookeeper3.4.6+Tomcat8搭建SolrCloud集群
Solr5.5.4+Zookeeper3.4.6+Tomcat8搭建SolrCloud集群 SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力 ...
- Linux巨好用的
受不了了windows下配置烦死人.linux一站式搞定,无敌 安装python3 机子上py2 py3共存没问题 安装virtualenv (py3)分割实验环境非常稳 安装mysql 先安装了一 ...
- day37 08-Hibernate的反向工程
反向工程:先创建表,创建好表之后,就是持久化类和映射文件可以不用你写,而且你的DAO它也可以帮你生成.但是它生成的DAO可能会多很多的方法.你可以不用那么多方法,但是它里面提供了这种的.用hibern ...
- linux查看现在在运行的进程 $ pstree -a$ ps aux
这都是查看现有进程的. ps aux 的结果比较杂乱 pstree -a 的结果比较简单明了,可以看到正在运行的进程及相关用户.
- 【洛谷P1827】【USACO】 美国血统 American Heritage 由二叉树两个序列求第三个序列
P1827 美国血统 American Heritage 题目描述 农夫约翰非常认真地对待他的奶牛们的血统.然而他不是一个真正优秀的记帐员.他把他的奶牛 们的家谱作成二叉树,并且把二叉树以更线性的&q ...
- 怎么让一个不定宽高的div垂直水平居中?
方法一:使用CSS3 transform 父盒子设置:position:relative; div设置:position:absolute;transform:translate(-50%,-50%) ...
- linux常用目录介绍
bin:全称binary,含义是二进制,该目录中存放的都是一些二进制文件,这些文件都是可以被运行的. dev:该目录主要存放的是外接设备,例如:光盘等.注意:在其中的设备是不能被直接使用的,需要挂载( ...
- SpingBoot myBatis neo4j整合项目案例
此项目为spring boot - myBatis - neo4j数据库整合项目. 有增删改查(节点关系).动态分页条件排序等一些示例. git下载地址:git clone https://githu ...

