UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)
UVA 11534 - Say Goodbye to Tic-Tac-Toe
题意:给定一个序列,轮流放XO,要求不能有连续的XX或OO。最后一个放的人赢。问谁赢
思路:sg函数。每一段...看成一个子游戏,利用记忆化求sg值,记忆化的状态要记录下左边和右边是X还是O就可以
代码:
#include <stdio.h>
#include <string.h> const int N = 105;
int t, sg[3][3][N];
char str[N]; int getnum(char c) {
if (c == 'X') return 1;
if (c == 'O') return 2;
} int mex(int s, int e, int l) {
if (sg[s][e][l] != -1) return sg[s][e][l];
if (l == 0) return sg[s][e][l] = 0;
bool vis[N];
memset(vis, false, sizeof(vis));
for (int i = 1; i <= l; i++) {
for (int j = 1; j <= 2; j++) {
if (i == 1 && s == j) continue;
if (i == l && e == j) continue;
int t = mex(s, j, i - 1)^mex(j, e, l - i);
vis[t] = true;
}
}
for (int i = 0; ;i++)
if (!vis[i]) return sg[s][e][l] = i;
} int main() {
memset(sg, -1, sizeof(sg));
scanf("%d", &t);
while (t--) {
scanf("%s", str); int len = strlen(str), s = 0, e = 0, l = 0, ans = 0, cnt = 0;
for (int i = 0; i < len; i++) {
if (str[i] == '.')
l++;
else {
e = getnum(str[i]);
ans ^= mex(s, e, l);
s = e; l = 0; cnt++;
}
}
ans ^= mex(s, 0, l);
if (cnt&1)
ans = (ans == 0?1:0);
printf("%s\n", ans? "Possible.":"Impossible.");
}
return 0;
}
UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)的更多相关文章
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...
- POJ 2361 Tic Tac Toe
题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...
- 【leetcode】1275. Find Winner on a Tic Tac Toe Game
题目如下: Tic-tac-toe is played by two players A and B on a 3 x 3 grid. Here are the rules of Tic-Tac-To ...
- 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe
题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...
- UVA 10561 - Treblecross(博弈SG函数)
UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...
- [CareerCup] 17.2 Tic Tac Toe 井字棋游戏
17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...
- Epic - Tic Tac Toe
N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...
- python 井字棋(Tic Tac Toe)
说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...
- ACM-Team Tic Tac Toe
我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...
随机推荐
- 理解OCI(Open Container Initiative)及docker的OCI实现(转)
OCI定义了容器运行时标准,runC是Docker按照开放容器格式标准(OCF, Open Container Format)制定的一种具体实现. runC是从Docker的libcontainer中 ...
- 打印sql语句方法
var_dump($this->blackpool_model->getLastSql());
- C#汉字转十六进制
public class chsHex{/// <summary>/// 从汉字转换到16进制/// </summary>/// <param name="s& ...
- (7)case语句
(1)case 语法 case "变量" in 模式1) 命令序列1 ;; 模式2) 命令序列2 ;; 模式3) 命令序列3 ;; *) 无匹配后命令序列 esac (2)多系统配 ...
- Codeforces 651 C. Watchmen-曼哈顿距离和欧几里得距离
C. Watchmen time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...
- Codeforces 538 C. Tourist's Notes
C. Tourist's Notes time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- CF GukiZ hates Boxes 【二分+贪心】
Professor GukiZ is concerned about making his way to school, because massive piles of boxes are bloc ...
- 状压DP【p2622】 关灯问题II
题目描述--->P2622 关灯问题II 没用的话: 首先第一眼看到题,嗯?n<=10?搜索? 满心欢喜地敲了一通搜索. 交上去,Wa声一片? 全部MLE! 这么坑人神奇? 一想,可能是爆 ...
- 浅析module.exports和exports区别和使用
module.exports和exports 写node的时候,特别是自定义模块的时候,都是一顿乱敲,然后module.exports={}完事. 但有时候去看别人写的代码的时候会发现还可以expor ...
- [UOJ300]吉夫特
直接上lucas定理,可以得到$\binom nm=1$等价于$m$是$n$的子集(二进制) 因为数字两两不同,所以设$f_i$表示以$i$开头的满足要求的序列有多少个,转移就是$f_i\gets f ...