【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²的做法,但是竟然过了.就是枚举每一个点,然后旋转卡壳另外 ...
随机推荐
- Expression Blend4经验分享:制作一个简单的文字按钮样式
首先在Grid里放一个TextBlock,对象时间线窗口的结构树如下 右键点击grid,选择构成控件 会弹出构成控件的对话框,选择你要构成的控件类型,控件名称,控件样式存储位置 这里我们选择butto ...
- asp.net、 mvc session影响并发
现象:在一个网站中,当访问一个处理比较耗时的页面(A页面),页面请求还没有返回时,此时再点击访问该网站的其他页面(B页面)会出现B页面很久都没有响应和返回,直到A页面输出返回数据时才开始处理B页面的请 ...
- 深入理解OOP(三):多态和继承(动态绑定和运行时多态)
在前面的文章中,我们介绍了编译期多态.params关键字.实例化.base关键字等.本节我们来关注另外一种多态:运行时多态, 运行时多态也叫迟绑定. 深入理解OOP(一):多态和继承(初期绑定和编译时 ...
- Oracle中修改表名遇到“ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超时失效”
Oracle 11g中想修改表名: rename ASSETPORJECT to ASSETPROJECT; 结果提示:ORA-00054: 资源正忙, 但指定以 NOWAIT 方式获取资源, 或者超 ...
- Android开发环境部署
引言 在windows系统中安装Android的开发环境,将分为五个步骤来完成: 第一步:安装JDK 第二步:配置Windows上JDK的变量环境 第三步: 下载安装Eclipse 第四步:下载安 ...
- SDRAM读写一字(上)
SDRAM读写一字 系统设计 SDRAM指令 指令 常量名 CKE CSn RAS CASn WEn 备注 空操作 NOP 1 0 1 1 1 行激活 ACTIVE 1 0 0 1 1 读操作 ...
- nios II--实验3——led 100M软件部分
软件开发 参照实验二(led),该实验与实验二(led)的不同之处在于系统的时钟由50M提成为100M.运行结果,在调试窗口输出Hello from Nios II!,并且板上的四个LED灯流动显示, ...
- 屠龙之路_战胜View&对DataBase猛烈进攻_ThirdDay
避开狮身人面兽攻击的屠龙战士继续前行.突然眼见一黑,战士们进到迷宫里,机智的战士用一根羊毛线绑在迷宫入口处,少年们手握着羊毛线,在迷宫里继续前行.在那样一个拐角处,遇到了恶龙的手下View&D ...
- 【The Expendables】团队博客目录
站立式会议: •[alpha版本]第一次站立式会议 •[beta版本]冲刺计划 •[beta版本]冲刺总结 •[alpha版本]第二次站立式会议 •[beta版本]第 ...
- js原型继承的几种方式
1. 原型链继承 2,构造函数继承(对象冒充继承) 3,组合继承(原型链继承+构造函数继承) 4,原型式继承 5. 寄生组合式继承 一.原型链继承 function Show(){ this.name ...