UVA10838 The Pawn Chess
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- cf493D Vasya and Chess
D. Vasya and Chess time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- Gerald and Giant Chess
Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- CF559C Gerald and Giant Chess
题意 C. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes input ...
- E. Gerald and Giant Chess
E. Gerald and Giant Chess time limit per test 2 seconds memory limit per test 256 megabytes2015-09-0 ...
- 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 ...
随机推荐
- git 入门教程之配置 git
配置 git 安装完成后,还需要最后一步配置就可以愉快使用了,在命令行输入: git config --global user.name "your username" git c ...
- C#调用原生C++ COM对象(在C++中实现C#的接口)
为了跨平台在.net core中使用COM,不能使用Windows下的COM注册机制,但是可以直接把IUnknown指针传给C#,转换为指针,再转换为C#的接口(interface). 做了这方面的研 ...
- 添加用户到sudoers
** is not in the sudoersfile. This incident will bereported.” (用户不在sudoers文件中……) 处理这个问题很简单,但应该先理解其原 ...
- 使用Visual Studio Team Services持续集成(二)——为构建定义属性
使用Visual Studio Team Services持续集成(二)--为构建定义属性 1.从VSTS帐户进入到Build 2.编辑构建定义并单击Options Description:如果这里明 ...
- 结对编程--四则运算(Java)萧英杰 夏浚杰
结对编程--四则运算(Java)萧英杰 夏浚杰 Github项目地址 功能要求 题目:实现一个自动生成小学四则运算题目的命令行程序 使用 -n 参数控制生成题目的个数(实现) 使用 -r 参数控制题目 ...
- LeetCode题解之Binary Tree Right Side View
1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if ...
- C#-hello world(二)
1.C# 程序构成 命名空间(Namespace) 一个 class Class 方法 Class 属性 一个 Main 方法 语句(Statements)和 表达式(Expressions) 注释 ...
- 洗礼灵魂,修炼python(76)--全栈项目实战篇(4)—— 购物车系统
要求: 1.基本符合日常购物车的要求(根据你的想法开放性提升功能) 2.展示商品信息,并且可随时上新商品 3.用户购买每一样商品时都对所剩的钱做一次对比,如果够则提示“已购买”,如果不够提示“余额不足 ...
- Unity与安卓开发的一些路径知识
APK安装之后找不到路径 公司的测试机(安卓)基本都是不带SD卡的. APK在安卓手机上安装之后,使用手机助手类的软件打开文件管理,打开 内置SDK卡/Android/data/ 在这个目录下却发现 ...
- C#のsocket通信
博主要做一个手机和电脑端(C#)通讯的程序,便览了网络上关乎socket的东西.但是接收文件的时候卡住了,怎么也接收不全.后来做了分片处理,如果分片,发送的时候就会有不同的socket(客户端开发不是 ...