题目链接  Hrbust 2320

用三进制来存储整个棋盘的状态。

设$dp[status][now]$为轮到$now$下棋的时候是必胜必败还是平局。

那么若当前能延伸出的所有状态中存在必败态的,则当前状态为必胜态。

否则看所有延伸出的所有状态中是否存在一个平局态,如果存在则当前状态为平局。

否则当前状态为必败态。

平局局面:当棋盘已经被下满并且没有任意$3$个棋子连起来的时候则平局。

#include <bits/stdc++.h>

using namespace std;

#define rep(i, a, b)	for (int i(a); i <= (b); ++i)
#define dec(i, a, b) for (int i(a); i >= (b); --i) const int N = 2e4 + 10;
const char *sss = ".ox~"; int a[3][3];
int f[N][2];
char ch[2];
int T;
int s, x;
int ans; int calc(int a[3][3]){
int ret = 0;
rep(i, 0, 2){
rep(j, 0, 2){
ret = ret * 3 + a[i][j];
}
} return ret;
} int solve(int s, int x){
if (~f[s][x]) return f[s][x];
int c[3][3];
memset(c, 0, sizeof c); int xx = s;
dec(i, 2, 0){ dec(j, 2, 0){ c[i][j] = xx % 3; xx /= 3; }} if (c[0][0] && c[0][0] == c[0][1] && c[0][1] == c[0][2] && c[0][0] != x) return f[s][x] = 1;
if (c[1][0] && c[1][0] == c[1][1] && c[1][1] == c[1][2] && c[1][0] != x) return f[s][x] = 1;
if (c[2][0] && c[2][0] == c[2][1] && c[2][1] == c[2][2] && c[2][0] != x) return f[s][x] = 1;
if (c[0][0] && c[0][0] == c[1][0] && c[1][0] == c[2][0] && c[2][0] != x) return f[s][x] = 1;
if (c[0][1] && c[0][1] == c[1][1] && c[1][1] == c[2][1] && c[2][1] != x) return f[s][x] = 1;
if (c[0][2] && c[0][2] == c[1][2] && c[1][2] == c[2][2] && c[2][2] != x) return f[s][x] = 1;
if (c[0][0] && c[0][0] == c[1][1] && c[1][1] == c[2][2] && c[0][0] != x) return f[s][x] = 1;
if (c[0][2] && c[0][2] == c[1][1] && c[1][1] == c[2][0] && c[0][2] != x) return f[s][x] = 1; int yy = 0;
rep(i, 0, 2) rep(j, 0, 2) if (c[i][j] == 0) yy = 1;
if (!yy) return f[s][x] = 0; int ff[11];
memset(ff, 0, sizeof ff);
int cnt = 0;
rep(i, 0, 2){
rep(j, 0, 2){
if (c[i][j]) continue;
c[i][j] = x;
int ss = calc(c);
++cnt;
ff[cnt] = solve(ss, 3 - x);
c[i][j] = 0;
}
} int ret0 = 0, ret2 = 0;
rep(i, 1, cnt){
if (ff[i] == 1) ret2 = 1;
else if (ff[i] == 0) ret0 = 1;
} if (ret2) return f[s][x] = 2;
else if (ret0) return f[s][x] = 0;
else return f[s][x] = 1;
} void init(){
rep(i, 0, 2){
rep(j, 0, 2){
scanf("%s", ch);
if (ch[0] == '.') a[i][j] = 0;
else if (ch[0] == 'o') a[i][j] = 1;
else if (ch[0] == 'x') a[i][j] = 2;
}
} scanf("%s", ch);
if (ch[0] == 'o') x = 1;
else x = 2;
s = calc(a);
} int main(){ memset(f, -1, sizeof f); scanf("%d", &T);
while (T--){
init();
ans = solve(s, x);
if (x == 2){
if (ans == 2) puts("x win!");
else if (ans == 1) puts("o win!");
else puts("tie!");
} else{
if (ans == 2) puts("o win!");
else if (ans == 1) puts("x win!");
else puts("tie!");
}
} return 0;
}

  

Hrbust 2320 OX (博弈)的更多相关文章

  1. 【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move

    poj  1568:Find the Winning Move   [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cas ...

  2. hdu----(1849)Rabbit and Grass(简单的尼姆博弈)

    Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. HDU 5754 Life Winner Bo 组合博弈

    Life Winner Bo Problem Description   Bo is a "Life Winner".He likes playing chessboard gam ...

  4. HDU 2509 Nim博弈变形

    1.HDU 2509  2.题意:n堆苹果,两个人轮流,每次从一堆中取连续的多个,至少取一个,最后取光者败. 3.总结:Nim博弈的变形,还是不知道怎么分析,,,,看了大牛的博客. 传送门 首先给出结 ...

  5. HDU 1907 Nim博弈变形

    1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形. ...

  6. 51nod1072(wythoff 博弈)

    题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1072 题意: 中文题诶~ 思路: 博弈套路是有的, 找np局 ...

  7. ACM: NBUT 1107 盒子游戏 - 简单博弈

     NBUT 1107  盒子游戏 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format:  Practice  Appoint ...

  8. 【转】ACM博弈知识汇总

    博弈知识汇总 转自:http://www.cnblogs.com/kuangbin/archive/2011/08/28/2156426.html 有一种很有意思的游戏,就是有物体若干堆,可以是火柴棍 ...

  9. Uva 10891 经典博弈区间DP

    经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...

随机推荐

  1. Android Studio 快捷键(包含自定义)终极版

      [F] [F] F2 在错误代码之间切换 F3 往前定位(Shift + F3:往后定位 )有问题 F4\Ctrl+鼠标点击\Ctrl+B 转到定义,查看类继承关系 F5 但不调试进入函数内部. ...

  2. FreeMarker的基础语法使用 && 心得和技巧

    FreeMarker语言 FreeMarker语言概述 FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写. FreeMarker被设计用来生成HTML Web ...

  3. 【Jump Game II 】cpp

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  4. 【Remove Elements】cpp

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  5. linux环境搭建系列之Apache MQ安装

    1.创建文件夹 #mkdir MQ 2.解压 #tar -vxf apache-activemq-5.14.3-bin.tar.gz 3.进入解压后的目录 # cd apache-activemq-5 ...

  6. MOTCF 没时间解释了 条件竞争漏洞

    moctf 没时间解释了 条件竞争漏洞 题目链接 条件竞争: 在本题目中,上传文件的时候服务器无条件的接收任何类型的文件,但是你上传之后服务器会给你的文件内容修改为too slow. 比如你上传了一句 ...

  7. 安恒杯月赛 babypass getshell不用英文字母和数字

    BABYBYPASS 先贴代码: ①限制字符长度35个 ②不能使用英文字母和数字和 _ $ 最后提示有个getFlag()函数,从这个函数入手. 我们的第一思路是直接eval执行getFlag函数,但 ...

  8. python-day4-装饰器的使用

    摘要:某公司的基础开发平台,有大概N多个函数,boss要求小A,为每个函数添加权限验证功能,而且要求不得修改函数内部结构,让小A尝试从代码外部入手,作为新手小A来讲,这无疑是一个巨大的工作量,难道TM ...

  9. vim中插入递增数

    假设生成0-9的递增数 1.插入数字1,yy复制,9p 2.输入命令 let i= | g//s//\=i/ | let i=i+1 3.结果:

  10. c++矩阵

    这里讲的矩阵有创建矩阵,矩阵加法,矩阵乘法,输出矩阵这些功能. #include<iostream> using namespace std; template<class T> ...