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可以从这串数字的两端任意选数字,一次只能 ...
随机推荐
- 问题 1936: [蓝桥杯][算法提高VIP]最大乘积
问题 1936: [蓝桥杯][算法提高VIP]最大乘积 时间限制: 1Sec 内存限制: 128MB 提交: 77 解决: 16 题目描述 对于n个数,从中取出m个数,如何取使得这m个数的乘积最大呢? ...
- Java的内存回收
一.java引用的种类 1.对象在内存中的状态 可达状态:当一个对象被创建后,有一个以上的引用变量指向它. 可恢复状态: 不可达状态:当对象的所有关联被切断,且系统调用所有对象的finalize方法依 ...
- 在Foxmail中添加阿里云企业邮箱账号
1.安装完成Foxmail之后,新建账号 输入阿里云邮箱地址和密码,点击创建 接受服务器类型你可以选择POP3或者IMAP,在这里我选择的是POP3 点击创建,大功告成. 为什么要写这篇文章呢? 因为 ...
- PostgreSql基础命令及问题总结
本章内容: 1.基本命令 基本命令 1.psql -U cdnetworks_beian -d cdnetworks_beian #-U指定用户,-d指定数据库 2.\l ...
- 【Trapping Rain Water】cpp
题目: Given n non-negative integers representing an elevation map where the width of each bar is 1, co ...
- MongoDB快速入门学习笔记5 MongoDB的文档修改操作
db.集合名称.update({query},{update},upsert, multi})query:过滤条件update:修改内容upsert:如果不存在查询条件查出的记录,是否插入一条数据,默 ...
- Python中@property和@classmethod和@staticmethod
前戏 首先,先要弄清楚一个类里面的,各个组成部分都应该怎么称呼. - 注:可能叫法会不太一样. 关于@property 顾名思义:它的意思为‘属性’. 作用: 1:使用它你将会把类方法,变为类属性.并 ...
- Leetcode 652.寻找重复的子树
寻找重复的子树 给定一棵二叉树,返回所有重复的子树.对于同一类的重复子树,你只需要返回其中任意一棵的根结点即可. 两棵树重复是指它们具有相同的结构以及相同的结点值. 下面是两个重复的子树: 因此,你需 ...
- Leetcode 521.最长特殊序列I
最长特殊序列 I 给定两个字符串,你需要从这两个字符串中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列). 子序列可以通过删去字符串中的某些字符 ...
- LeetCode with Python -> Dynamic Programming
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...