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. 032医疗项目-模块三:药品供应商目录模块——供货商药品目录查询功能----------Service层和Action层和调试

    我们上一篇文章讲了Dao层代码: 这一篇我们讲解Service层和Action层: Service层: 分为接口和实现类,我们主要看实现类:GysemplServiceImpl package yyc ...

  2. SQL Server 2005、2008 的 datetime 值范围(转)

    SQL Server 2005.2008 的 datetime 最小值是:1753-01-01 00:00:00 最大值是:9999-12-31 23:59:59.997 这与 .NET 中的 Dat ...

  3. C#.NET 大型通用信息化系统集成快速开发平台 4.0 版本 - 拆分表、联系方式的拆分?

    当用户数据有接近10万时,而且多表的关联也比较频繁时,能把大表拆为小表,也会提高系统的性能,I/O.运算性能.当然以后用户数据会更大可能会到30-40万以上,所有有能力时适当拆表,分分合合,合合分分也 ...

  4. 全球第一本基于Bootstrap V3.x的图书《深入理解Bootstrap》终于上市了,再次免费送书15本【活动结束】

    先说活动规则,再说书的事 经过将近1年的努力,终于有了第一本自己独立编写的书:<深入理解Bootstrap>,基于最新版V 3.1 ,侧重于源码详解.架构分析.插件扩展(全新开发)实战.为 ...

  5. spring mvc4:异常处理

    前面学习过struts2的异常处理,今天来看下spring mvc4的异常处理: 一.Servlet配置文件修改 <bean id="exceptionResolver" c ...

  6. fdisk分区硬盘并shell脚本自动化

    最近工作需要用到对硬盘进行shell脚本自动化分区和mount的操作,google了一些资料,下面做个总结. 如果硬盘没有进行分区(逻辑分区或者扩展分区,关于两者概念,自行google),我们将无法将 ...

  7. win8/8.1 免密码登录设置

    http://www.ehow.com/how_8013338_start-windows-password.html 原文见上方链接 1,win+r调出命令输入窗口,输入 netplwiz 回车 2 ...

  8. [CF #236 (Div. 2) E] Strictly Positive Matrix(强联通分量)

    题目:http://codeforces.com/contest/402/problem/E 题意:给你一个矩阵a,判断是否存在k,使得a^k这个矩阵全部元素都大于0 分析:把矩阵当作01矩阵,超过1 ...

  9. C# 如何捕获键盘按钮和组合键以及KeyPress/KeyDown事件之间的区别 (附KeyChar/KeyCode值)

    1. 首先将窗口属性KeyPreview设为true,如果属性对话框中找不到,就直接在代码里添加: 2. 添加KeyPress / KeyDown事件: 1.KeyPress 和KeyDown .Ke ...

  10. 用户 'IIS APPPOOL\***' 登录失败

    用户 'IIS APPPOOL\DefaultAppPool' 登录失败. 我在windows8中安装了iis之后添加了我做的网站打开之后提示用户 'IIS APPPOOL\DefaultAppPoo ...