【poj1085】 Triangle War
http://poj.org/problem?id=1085 (题目链接)
题意
A,B两人玩游戏,在一个大三角形上放火柴,若A放上一根火柴后成功组成一个三角形,那么这个三角形就归属于A,并且A被奖励再放一根火柴。最后谁三角形多谁就胜。
给出一个残局,判断是否存在先手必胜策略。
Solution
最近一直在颓,好久没刷题了。。。
这就是神乎其技的极大极小搜索,其实也差不多就是个贪心,基本很少用上,因为很难判断估价函数的正确性。。详情请见:http://blog.csdn.net/gwq5210/article/details/48163539。
极大极小搜索就是专门用来解决这一类问题的。在这道题中,我们先对于每一个火柴所放置的位置以及由3根火柴组成的三角形打一个表,将初始状态模拟出来。之后进行搜索,我们把 A的三角形个数-B的三角形个数 当做估价函数。而如果当前节点x与其父亲节点为同一个人决策时,x传承其父亲的alpha或者是beta。然后就是套模板了。
代码
// poj1085
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<queue>
#define MOD 100003
#define inf 2147483640
#define LL long long
#define free(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout);
using namespace std;
/// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
int e[][2]={{1,2},{1,3},{2,4},{2,5},{2,3},{3,5},{3,6},{4,5},{4,7},{4,8},{5,6},{5,8},{5,9},{6,9},{6,10},{7,8},{8,9},{9,10}};
int a[][3]={{0,1,4},{2,3,7},{3,4,5},{5,6,10},{8,9,15},{7,9,11},{11,12,16},{10,12,13},{13,14,17}};
int n,cnt,f[20]; int getid(int x,int y) {
for (int i=0;i<18;i++)
if ((e[i][0]==x && e[i][1]==y) || (e[i][0]==y && e[i][1]==x)) return i;
return -1;
}
int cal() {
int tt=0;
for (int i=0;i<9;i++) if (f[a[i][0]] && f[a[i][1]] && f[a[i][2]]) tt++;
return tt;
}
int maxdfs(int beta,int a,int b);
int mindfs(int alpha,int a,int b) {
if (cnt==18) return a>b ? inf : -inf;
if (a>=5) return inf;
if (b>=5) return -inf;
int tmp=inf;
for (int i=0;i<18;i++) if (!f[i]) {
f[i]=1;cnt++;
int c=cal();
if (c>a+b) tmp=min(mindfs(alpha,a,c-a),tmp);
else tmp=min(maxdfs(tmp,a,b),tmp);
f[i]=0;cnt--;
if (tmp<=alpha) return tmp;
}
return tmp;
}
int maxdfs(int beta,int a,int b) {
if (cnt==18) return a>b ? inf : -inf;
if (a>=5) return inf;
if (b>=5) return -inf;
int tmp=-inf;
for (int i=0;i<18;i++) if (!f[i]) {
f[i]=1;cnt++;
int c=cal();
if (c>a+b) tmp=max(maxdfs(beta,c-b,b),tmp);
else tmp=max(mindfs(tmp,a,b),tmp);
f[i]=0;cnt--;
if (tmp>=beta) return tmp;
}
return tmp;
}
int main() {
int T,t=0;scanf("%d",&T);
while (T--) {
scanf("%d",&n);
memset(f,0,sizeof(f));
cnt=n;
int w=0,ans[2]={0,0};
for (int x,y,i=1;i<=n;i++) {
scanf("%d%d",&x,&y);
int id=getid(x,y);
f[id]=1;
int c=cal();
if (c>ans[0]+ans[1]) ans[w]+=c-ans[0]-ans[1];
else w^=1;
}
int res=0;
if (!w) res=maxdfs(inf,ans[0],ans[1]);
else res=mindfs(-inf,ans[0],ans[1]);
printf("Game %d: %c wins.\n",++t,res==inf ? 'A' : 'B');
}
return 0;
}
【poj1085】 Triangle War的更多相关文章
- 【LeetCode】Triangle 解决报告
[称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...
- 【贪心】【Uva11729】 Commando War
你有n个部下,每个部下需要完成一项任务.第i个部下需要你花Bi分钟交待任务,然后他会立刻独立地.无间断地执行Ji分钟后完成任务.你需要选择交待任务的顺序,使得所有任务尽早执行完毕(即最后一个执行完的任 ...
- 【leetcode】Triangle (#120)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【leetcode】triangle(easy)
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【Leetcode】Triangle
给定一个由数字组成的三角形,从顶至底找出路径最小和. Given a triangle, find the minimum path sum from top to bottom. Each step ...
- 【Maven】构建war包时排除web.xml
在使用maven构建项目的war包时,有时并不需要src/webapp/WEB-INF/下的一些文件. 这时可以通过maven-war-plugin创建配置来排除这些文件.下面贴出我平时使用的pom. ...
- 【数组】Triangle
题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...
- 【Leetcode】【Medium】Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- 【poj2079】 Triangle
http://poj.org/problem?id=2079 (题目链接) 题意 求凸包内最大三角形面积 Solution 旋转卡壳. 只会n²的做法,但是竟然过了.就是枚举每一个点,然后旋转卡壳另外 ...
随机推荐
- PAT 1019. 数字黑洞 (20)
给定任一个各位数字不完全相同的4位正整数,如果我们先把4个数字按非递增排序,再按非递减排序,然后用第1个数字减第2个数字,将得到一个新的数字.一直重复这样做,我们很快会停在有"数字黑洞&qu ...
- Linux commands frequently used
touch <filename>.sh gedit <filename>.sh bash <filename>.sh & ps auxw|grep < ...
- removeNode is not defined removeNode is not a function
在javascript操作dom树的时候可能会经常遇到增加,删除节点的事情,比如一个输入框后一个增加按钮,一个删除按钮,点击增加就增加 个输入框,点击删除就删除对应的输入框.在一些js框架,如Prot ...
- DEDECMS之六 网站地图、RSS地图
在用织梦CMS做网站的都知道,在它的robots.txt是屏蔽掉了data目录的,可是,不巧dedecms默认的网站地图是在data下的,为了让蜘蛛更好的爬行,有必要将dedecms生成的网站地图放在 ...
- css position, display, float 内联元素、块级元素
position属性:position属性指出一个元素的定位方法.有4种可能值:static, relative, absolute or fixed: static:默认值,元素按照在文档流中出现的 ...
- Oracle字符分隔函数(split)
为了让 PL/SQL 函数返回数据的多个行,必须通过返回一个 REF CURSOR 或一个数据集合来完成.REF CURSOR 的这种情况局限于可以从查询中选择的数据,而整个集合在可以返回前,必须进行 ...
- Qt学习笔记 QMessageBox
Qt的几种MessageBox 1.Infomation类型 QMessageBox::information(this,tr("hello"),tr("title&qu ...
- Linux学习笔记-Ubuntu添加右键菜单打开终端
1.进入个人目录(如/home/batsing,下文缩写成 ~ ):设置显示隐藏文件,或使用命令行:2.进入 ~/.gnome2/nautilus-scripts 文件夹,新建一个文件,名为 term ...
- Qt环境搭建(Qt Creator)+Visual Studio
1.http://www.cnblogs.com/ranjiewen/p/5318768.html 简述 经常有人问我编写Qt程序时使用什么IDE,其实这个真的很难回答(各有所长),只能说看个人爱好了 ...
- Realm Java的学习、应用、总结
从React Native珠三角沙龙会议了解到Realm这个开源库,然后开始学习.理解和使用Realm.Realm是跨平台.支持多种主流语言,这里主要是对Realm Java结合实际项目的一些情况进行 ...