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的更多相关文章

  1. 【LeetCode】Triangle 解决报告

    [称号] Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjac ...

  2. 【贪心】【Uva11729】 Commando War

    你有n个部下,每个部下需要完成一项任务.第i个部下需要你花Bi分钟交待任务,然后他会立刻独立地.无间断地执行Ji分钟后完成任务.你需要选择交待任务的顺序,使得所有任务尽早执行完毕(即最后一个执行完的任 ...

  3. 【leetcode】Triangle (#120)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  4. 【leetcode】triangle(easy)

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  5. 【Leetcode】Triangle

    给定一个由数字组成的三角形,从顶至底找出路径最小和. Given a triangle, find the minimum path sum from top to bottom. Each step ...

  6. 【Maven】构建war包时排除web.xml

    在使用maven构建项目的war包时,有时并不需要src/webapp/WEB-INF/下的一些文件. 这时可以通过maven-war-plugin创建配置来排除这些文件.下面贴出我平时使用的pom. ...

  7. 【数组】Triangle

    题目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjace ...

  8. 【Leetcode】【Medium】Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  9. 【poj2079】 Triangle

    http://poj.org/problem?id=2079 (题目链接) 题意 求凸包内最大三角形面积 Solution 旋转卡壳. 只会n²的做法,但是竟然过了.就是枚举每一个点,然后旋转卡壳另外 ...

随机推荐

  1. Cordova - 使用Cordova开发iOS应用实战1(配置、开发第一个应用)

    Cordova - 使用Cordova开发iOS应用实战1(配置.开发第一个应用) 现在比较流行使用 html5 开发移动应用,毕竟只要写一套html页面就可以适配各种移动设备,大大节省了跨平台应用的 ...

  2. Linux shell特性

    一:别名 .alias 查看本用户下的alias配置 --自定义别名:alias 别名='shell命令' (注意是单引号) --cat $HOME/.bashrc 在这个用户下配置着alias名的配 ...

  3. Map集合 总结

    (本人第一次写博客,部分内容有参照李刚老师的疯狂java系列图书,如有遗漏错误,请多指教,谢谢.) Java的集合类可分为Set.List.Map.Queue,其中Set.List.Queue都有共同 ...

  4. mSites and Smarty

    目前的页面实现方式是需要向后台请求接口,返回 JSON 数据,动态拼接字符串塞进 DOM 中(innerHTML).考虑用 Smarty 生成静态页面,可以在后台用 PHP 得到数据,字符串拼接,然后 ...

  5. WPF依赖属性

    原文:http://www.cnblogs.com/xiongpq/archive/2010/06/29/1767905.html 概述: Windows Presentation Foundatio ...

  6. c#类库和可移值类库的区别

    所谓类库,只能指定一个类库的可运行平台. 而可移值类库,可以在无需修改代码的情况,同时可以在多平台上运行DLL文件.多平台如NET Framework.Silverlight.Windows Phon ...

  7. memcache 安装

    1 下载两个文件 wget http://www.danga.com/memcached/dist/memcached-1.2.0.tar.gz wget http://www.monkey.org/ ...

  8. apache 伪静态配置

    1 现配置站点, <VirtualHost *:80> //访问名称 ServerName my.seo_demo.com //项目地址 DocumentRoot "/Users ...

  9. 屠龙之路_向恶龙Alpha进发_FirstDay

    听说山的那边海的那边,出现了一头名为Alpha的恶龙,此龙无恶不作,还掠走了国王那漂酿的公主.少年很是气愤,大吼:"放开那女孩!!!",于是找到了志同道合的六位勇士,一起组成了屠龙 ...

  10. Beta版本冲刺———第三天

    会议照片: 项目燃尽图: 1.项目进展: 今天解决的进度:对游戏结束的检测进行了完善,使分数标签和最高分标签的变化更加合理. 仍在进行对排行榜分数变更的实现 2.每个人每天做的事情 郭怡锋:汇总工作进 ...