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

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

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

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

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

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

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

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

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

CODE

  1. #include<cstdio>
  2. #include<vector>
  3. #define RI register int
  4. #define pb push_back
  5. using namespace std;
  6. const int N=4,INF=1e9;
  7. struct status
  8. {
  9. char a[N+1][N+1]; //0 white turn,1 black turn
  10. inline int isover(void)
  11. {
  12. for (RI i=0;i<N;++i)
  13. {
  14. if (a[0][i]=='P') return 0;
  15. if (a[3][i]=='p') return 1;
  16. } return -1;
  17. }
  18. inline void expand(int player,vector <status> &next)
  19. {
  20. if (!player)
  21. {
  22. for (RI i=1;i<4;++i) for (RI j=0;j<4;++j) if (a[i][j]=='P')
  23. {
  24. if (a[i-1][j]=='.') { status to=*this; to.a[i-1][j]='P'; to.a[i][j]='.'; next.pb(to); }
  25. 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); }
  26. 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); }
  27. }
  28. } else
  29. {
  30. for (RI i=0;i<3;++i) for (RI j=0;j<4;++j) if (a[i][j]=='p')
  31. {
  32. if (a[i+1][j]=='.') { status to=*this; to.a[i+1][j]='p'; to.a[i][j]='.'; next.pb(to); }
  33. 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); }
  34. 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); }
  35. }
  36. }
  37. }
  38. }st; int t;
  39. inline int AlphaBeta_Search(status now,int step,int player,int alpha,int beta)
  40. {
  41. int res=now.isover(); if (!res) return INF-step; if (~res) return step-INF;
  42. vector <status> next; next.clear(); now.expand(player,next);
  43. int lim=next.size(); if (!lim) return player?INF-step:step-INF;
  44. for (RI i=0;i<lim;++i)
  45. {
  46. res=AlphaBeta_Search(next[i],step+1,player^1,alpha,beta);
  47. if (player) beta=res<beta?res:beta; else alpha=res>alpha?res:alpha;
  48. if (beta<=alpha) break;
  49. }
  50. return player?beta:alpha;
  51. }
  52. int main()
  53. {
  54. for (scanf("%d",&t);t;--t)
  55. {
  56. for (RI i=0;i<4;++i) scanf("%s",st.a[i]);
  57. int res=AlphaBeta_Search(st,0,0,-2*INF,2*INF); //res<0 means black will win
  58. if (res<0) printf("black (%d)\n",res+INF); else printf("white (%d)\n",-res+INF);
  59. }
  60. return 0;
  61. }

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. ThreadLocal 类 的源码解析以及使用原理

    1.原理图说明 首先看这一张图,我们可以看出,每一个Thread类中都存在一个属性 ThreadLocalMap 成员,该成员是一个map数据结构,map中是一个Entry的数组,存在entry实体, ...

  2. C# SharpMap的简单使用

    本文是利用ShapMap实现GIS的简单应用的小例子,以供学习分享使用.关于SharpMap的说明,网上大多是以ShapeFile为例进行简单的说明,就连官网上的例子也不多.本文是自己参考了源代码进行 ...

  3. coTurn测试程序之 turnutils_uclient

    接着对使用coTurn搭建的STUN/TURN服务使用turnutils_uclient程序测试其TURN服务是否正常. 直接连接服务测试服务是否正常.为保证测试使用的服务是TURN服务,在TURN服 ...

  4. 一张图教你读懂AI简史

  5. Android内嵌PDF预览

    一.在对应模块的build.gradle文件中加入依赖 dependencies { implementation 'com.github.barteksc:android-pdf-viewer:3. ...

  6. Linux用户抢占和内核抢占详解(概念, 实现和触发时机)--Linux进程的管理与调度(二十)

    1 非抢占式和可抢占式内核 为了简化问题,我使用嵌入式实时系统uC/OS作为例子 首先要指出的是,uC/OS只有内核态,没有用户态,这和Linux不一样 多任务系统中, 内核负责管理各个任务, 或者说 ...

  7. 关于svn上传.classpath等问题

    1. svn版本:客户端版本与服务器版本 要尽量适配. 2. svn管理项目:有人将.classpath, .project, .mymetadata, .myumldata等文件也纳入到版本控制,如 ...

  8. shell linux 环境下循环遍历文件夹下所有文件

    demofun(){ ` do if test -f $file then echo "file: $file" elif test -d $file then echo &quo ...

  9. Linux 小知识翻译 - 「BitTorrent」

    这次聊聊「BitTorrent」. (也就是下片子的 BT) BitTorrent是文件传输的一种.它可以在服务端负担很小的情况下高速的传输文件. BitTorrent最大的特点就是服务端可以将文件的 ...

  10. K-means算法的matlab程序(初步)

    K-means算法的matlab程序 在https://www.cnblogs.com/kailugaji/p/9648369.html 文章中已经介绍了K-means算法,现在用matlab程序实现 ...