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函数)的更多相关文章

  1. 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 ...

  2. POJ 2361 Tic Tac Toe

    题目:给定一个3*3的矩阵,是一个井字过三关游戏.开始为X先走,问你这个是不是一个合法的游戏.也就是,现在这种情况,能不能出现.如果有人赢了,那应该立即停止.那么可以知道X的步数和O的步数应该满足x= ...

  3. 【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 ...

  4. 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 ...

  5. UVA 10561 - Treblecross(博弈SG函数)

    UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',能够在'.'的位置放X.谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每一个串要是上面有 ...

  6. [CareerCup] 17.2 Tic Tac Toe 井字棋游戏

    17.2 Design an algorithm to figure out if someone has won a game oftic-tac-toe. 这道题让我们判断玩家是否能赢井字棋游戏, ...

  7. Epic - Tic Tac Toe

    N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If ...

  8. python 井字棋(Tic Tac Toe)

    说明 用python实现了井字棋,整个框架是本人自己构思的,自认为比较满意.另外,90%+的代码也是本人逐字逐句敲的. minimax算法还没完全理解,所以参考了这里的代码,并作了修改. 特点 可以选 ...

  9. ACM-Team Tic Tac Toe

    我的代码: #include <bits/stdc++.h> using namespace std; int main() { char a[3][3]; int i,j=0; for( ...

随机推荐

  1. linux 设置svn钩子实现自动更新

    一.svn安装设置 1.安装svn启动 yum install subversion 2.建个svn的根目录,因为项目不止一个 mkdir -p /home/svn/3.新建一个新的空的版本仓库(su ...

  2. nodejs 服务器重新启动

    在 我们开发node 应用的时候,一但你的应用已经启动了,这个时候如果你修改了服务端的文件,那么要是这个修改起作用,你必须手动停止服务然后再重新启动,这在开发过程中无 疑是很烦人的一件事,最好是有一个 ...

  3. SPOJ CIRU - The area of the union of circles (圆的面积并)

    CIRU - The area of the union of circles no tags  You are given N circles and expected to calculate t ...

  4. HDU1385 Minimum Transport Cost (Floyd)

    Minimum Transport Cost Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/O ...

  5. BigDecimal不整除异常

    通过BigDecimal的divide方法进行除法时当不整除,出现无限循环小数时,就会抛异常的 异   常 :java.lang.ArithmeticException: Non-terminatin ...

  6. [BZOJ 2964] Boss单挑战

    Link:https://www.lydsy.com/JudgeOnline/problem.php?id=2964 Algorithm: 一道很新颖的背包问题 此题每个状态要维护的量巨多,而转移方式 ...

  7. [Lydsy1805月赛] 对称数

    挺不错的一道数据结构题QWQ. 一开始发现这个题如果不看数据范围的话,妥妥的树上莫队啊23333,然鹅10组数据是不可能让你舒舒服服的树上莫队卡过的23333 于是想了想,这个题的模型就是,把u到v链 ...

  8. 【斜率优化】BZOJ1096-[ZJOI2007]仓库建设

    [题目大意] 有n个工厂编号分别为1-n,第i个仓库库存量为p[i],距离第1个仓库的距离为x[i](x[1]=0).在每一个工厂建立一个仓库费用为c[i],没有建立仓库的工厂只能往编号大于它的仓库运 ...

  9. iOS中设置backBarButtonItem的title和action

    一. 设置title 在需要显示该返回键的前一个Controller中设置: 1: navigationItem.backBarButtonItem = UIBarButtonItem(title: ...

  10. 微软自家的.Net下的JavaScript引擎——ClearScript

    之前我介绍过一个开源的.Net下的Javascript引擎Javascript .NET,今天发现微软自己也开源了一个JavaScript引擎——ClearScript(当然,也支持VB Script ...