Hrbust 2320 OX (博弈)
题目链接 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 (博弈)的更多相关文章
- 【迭代博弈+搜索+剪枝】poj-1568--Find the Winning Move
poj 1568:Find the Winning Move [迭代博弈+搜索+剪枝] 题面省略... Input The input contains one or more test cas ...
- hdu----(1849)Rabbit and Grass(简单的尼姆博弈)
Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 5754 Life Winner Bo 组合博弈
Life Winner Bo Problem Description Bo is a "Life Winner".He likes playing chessboard gam ...
- HDU 2509 Nim博弈变形
1.HDU 2509 2.题意:n堆苹果,两个人轮流,每次从一堆中取连续的多个,至少取一个,最后取光者败. 3.总结:Nim博弈的变形,还是不知道怎么分析,,,,看了大牛的博客. 传送门 首先给出结 ...
- HDU 1907 Nim博弈变形
1.HDU 1907 2.题意:n堆糖,两人轮流,每次从任意一堆中至少取一个,最后取光者输. 3.总结:有点变形的Nim,还是不太明白,盗用一下学长的分析吧 传送门 分析:经典的Nim博弈的一点变形. ...
- 51nod1072(wythoff 博弈)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1072 题意: 中文题诶~ 思路: 博弈套路是有的, 找np局 ...
- ACM: NBUT 1107 盒子游戏 - 简单博弈
NBUT 1107 盒子游戏 Time Limit:1000MS Memory Limit:65535KB 64bit IO Format: Practice Appoint ...
- 【转】ACM博弈知识汇总
博弈知识汇总 转自:http://www.cnblogs.com/kuangbin/archive/2011/08/28/2156426.html 有一种很有意思的游戏,就是有物体若干堆,可以是火柴棍 ...
- Uva 10891 经典博弈区间DP
经典博弈区间DP 题目链接:https://uva.onlinejudge.org/external/108/p10891.pdf 题意: 给定n个数字,A和B可以从这串数字的两端任意选数字,一次只能 ...
随机推荐
- BFS:Open and Lock(一个数的逐位变化问题的搜索)
解体心得: 1.关于定义四维数组的问题,在起初使用时,总是在运行时出错,找了很多方法,最后全部将BFS()部分函数写在主函数中,将四维数组定义在主函数中才解决了问题.运行成功后再次将四维数组定义为全局 ...
- Diycode开源项目 LoginActivity分析
1.首先看一下效果 1.1.预览一下真实页面 1.2.分析一下: 要求输入Email或者用户名,点击编辑框,弹出键盘,默认先进入输入Email或用户名编辑框. 点击密码后,密码字样网上浮动一段距离,E ...
- 12,scrapy框架之post请求
今日概要 递归爬取解析多页页面数据 scrapy的post请求发送 1.递归爬取解析多页页面数据 - 需求:将糗事百科所有页码的作者和段子内容数据进行爬取切持久化存储 - 需求分析:每一个页面对应一个 ...
- 实验6 流类库与I/O
Part2 基础练习 使用文件I/O流,以文本方式打开Part1中合并后的文件,在文件最后一行添加字符"merge successfully. " // 合并两个文件内容到一个新文 ...
- 33、Android Support兼容包详解(转载)
原文转自:微信分享 2015-03-31 22:11 背景 来自于知乎上邀请回答的一个问题Android中AppCompat和Holo的一个问题?, 看来很多人还是对这些兼容包搞不清楚,那么干脆写篇博 ...
- mongo命令
安装mongo http://docs.mongodb.org/master/tutorial/install-mongodb-on-redhat-centos-or-fedora-linux/ 启动 ...
- python拼接
拼接: name=zhuhuan age=23 salary=333 info=''' ----- info of %s----- age:%s name:%s salary:%s %(name,ag ...
- pb8.0 mssqlserver 新建数据库连接问题
将ntwdblib.DLL复制到Sybase\Shared\PowerBuilder目录下 unable to load the requested database interface,无法创建数据 ...
- 基数排序(java实现)
基数排序 就是先比较数组中元素的个位数,排序得到新的数组,然后比较新的数组中的十位数,排序得到新数组,然后再对最新得到的数组比较百位数.......依次循环 比如{82 ,31 ,29 ,71, 7 ...
- ValueStack、ActionContext
笔者不知道该用哪个词来形容ValueStack.ActionContext等可以在Struts2中用来存放数据的类.这些类使用的范围不同,得到的方法也不同,下面就来一一介绍. 1. ValueStac ...