UVA好题没人写系列,感觉可以稍稍练习一下面向对象编程的形式(大雾)

题意很简单,在国际象棋的棋盘中有一些兵,走到对方底线即为胜利,问最优决策下谁能获胜。并输出最小步数。

首先这里的棋盘都只有\(4\times 4\),意味这状态很小。

所以我们可以联想到用类似于Luogu P4576 [CQOI2013]棋盘游戏对抗搜索的方式求解。

如果可以获胜就找最小步数,否则要失败的那一方应该找一个能苟活最久的状态走下去。

发现这个决策其实就是取\(\min,\max\),这里显然也可以记忆化,但状态数\(3^{16}\)加上多组数据实在带不动。

这个时候我们只能掏出对抗搜索的神奇剪枝了——\(alpha-beta\)剪枝。

原理和模板可以自行百度,其实就是一种最优性剪枝

注意合理的实现方式,否则可能会让代码又臭又长。

CODE

#include<cstdio>
#include<vector>
#define RI register int
#define pb push_back
using namespace std;
const int N=4,INF=1e9;
struct status
{
char a[N+1][N+1]; //0 white turn,1 black turn
inline int isover(void)
{
for (RI i=0;i<N;++i)
{
if (a[0][i]=='P') return 0;
if (a[3][i]=='p') return 1;
} return -1;
}
inline void expand(int player,vector <status> &next)
{
if (!player)
{
for (RI i=1;i<4;++i) for (RI j=0;j<4;++j) if (a[i][j]=='P')
{
if (a[i-1][j]=='.') { status to=*this; to.a[i-1][j]='P'; to.a[i][j]='.'; next.pb(to); }
if (j&&a[i-1][j-1]=='p') { status to=*this; to.a[i-1][j-1]='P'; to.a[i][j]='.'; next.pb(to); }
if (j<3&&a[i-1][j+1]=='p') { status to=*this; to.a[i-1][j+1]='P'; to.a[i][j]='.'; next.pb(to); }
}
} else
{
for (RI i=0;i<3;++i) for (RI j=0;j<4;++j) if (a[i][j]=='p')
{
if (a[i+1][j]=='.') { status to=*this; to.a[i+1][j]='p'; to.a[i][j]='.'; next.pb(to); }
if (j&&a[i+1][j-1]=='P') { status to=*this; to.a[i+1][j-1]='p'; to.a[i][j]='.'; next.pb(to); }
if (j<3&&a[i+1][j+1]=='P') { status to=*this; to.a[i+1][j+1]='p'; to.a[i][j]='.'; next.pb(to); }
}
}
}
}st; int t;
inline int AlphaBeta_Search(status now,int step,int player,int alpha,int beta)
{
int res=now.isover(); if (!res) return INF-step; if (~res) return step-INF;
vector <status> next; next.clear(); now.expand(player,next);
int lim=next.size(); if (!lim) return player?INF-step:step-INF;
for (RI i=0;i<lim;++i)
{
res=AlphaBeta_Search(next[i],step+1,player^1,alpha,beta);
if (player) beta=res<beta?res:beta; else alpha=res>alpha?res:alpha;
if (beta<=alpha) break;
}
return player?beta:alpha;
}
int main()
{
for (scanf("%d",&t);t;--t)
{
for (RI i=0;i<4;++i) scanf("%s",st.a[i]);
int res=AlphaBeta_Search(st,0,0,-2*INF,2*INF); //res<0 means black will win
if (res<0) printf("black (%d)\n",res+INF); else printf("white (%d)\n",-res+INF);
}
return 0;
}

UVA10838 The Pawn Chess的更多相关文章

  1. CodeForces 559C Gerald and Giant Chess

    C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  2. Codeforces Round #281 (Div. 2) D. Vasya and Chess 水

    D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. CF A and B and Chess

    A and B and Chess time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  4. cf493D Vasya and Chess

    D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. codeforces 519A. A and B and Chess,

    A. A and B and Chess time limit per test 1 second memory limit per test 256 megabytes input standard ...

  6. Gerald and Giant Chess

    Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  7. CF559C Gerald and Giant Chess

    题意 C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. E. Gerald and Giant Chess

    E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...

  9. Codeforces Round #313 (Div. 1) C. Gerald and Giant Chess DP

    C. Gerald and Giant Chess Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

随机推荐

  1. OkHttpUtils简单的网络去解析使用

    先添加依赖: implementation 'com.google.code.gson:gson:2.2.4' implementation 'com.zhy:okhttputils:2.0.0' 网 ...

  2. The value of ESP was not properly saved across a function call 快速解决

    The value of ESP was not properly...快速解决 今天遇到这个问题,真的是非常头疼,期间电脑居然崩掉一次.所以,分享一下解决办法. 如果是:类定义的时候,新添加了属性, ...

  3. 《JavaScript面向对象的编程指南》--读书笔记

    第一章.引言 1.5 面向对象的程序设计常用概念 对象(名词):是指"事物"在程序设计语言中的表现形式. 这里的事物可以是任何东西,我们可以看到它们具有某些明确特征,能执行某些动作 ...

  4. 机器学习之EM算法(五)

    摘要 EM算法全称为Expectation Maximization Algorithm,既最大期望算法.它是一种迭代的算法,用于含有隐变量的概率参数模型的最大似然估计和极大后验概率估计.EM算法经常 ...

  5. Linux中对逻辑卷进行扩容与缩小

    一.在扩容之前,先查看自己逻辑卷,卷组,物理卷的信息:(在上一篇的基础上:Linux中对逻辑卷的建立) 查看物理卷: # pvdisplay /dev/sdc1 查看卷组: vgdisplay /de ...

  6. springmvc复习笔记----springmvc最简单的第一个例子:RequestMapping试水

    结构 用到的包 web.xml <url-pattern>/</url-pattern>中可以换成其他的后缀*.do ,*. sb  …… <?xml version=& ...

  7. java单元测试

    单元测试 1.简介 在日常开发中,我们编写的任何代码都需要经过严谨的测试才可以发布.以往的测试方法都是通过编写一个main函数进行简单的测试,并使用大量的print语句输出结果,这种方法其实是不可取的 ...

  8. linux系统/var/log目录下的信息详解

    一./var目录 /var 所有服务的登录的文件或错误信息文件(LOG FILES)都在/var/log下,此外,一些数据库如MySQL则在/var/lib下,还有,用户未读的邮件的默认存放地点为/v ...

  9. shell的case用法

    今天给大家简单介绍一下结构条件语句的用法,实际上就是规范的多分支if语句,如下: case语法: case "字符串变量" in 值1)指令1... ;; 值2)指令2... ;; ...

  10. jQuery -- 光阴似箭(五):AJAX 方法

    jQuery -- 知识点回顾篇(五):AJAX 方法 1. $.ajax 方法:用于执行 AJAX(异步 HTTP)请求. <!DOCTYPE html> <html> &l ...