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. 第三篇:使用firewall-cmd

    本文来源:Working with firewalld         其它参考文档:redhat官方中文文档 1.查询类命令 2.设置类命令 3.运行时zone设置 4.永久性zone设置 5.直接 ...

  2. css深入理解之border

    1.  border-width border-width不支持百分比,类似的还有outline,box-shadow,text-shadow等 border-width支持关键字:thin(1px, ...

  3. hdu 1410(直线与矩形相交)

    Intersection Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13528   Accepted: 3521 Des ...

  4. 使用maven进行Javadoc下载

    project -> maven -> Download Sources and Download JavaDocs

  5. HDU 2473 Junk-Mail Filter 【并查集删除】

    Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. 2017 ACM-ICPC 亚洲区(青岛赛区)网络赛 1010

    #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #inclu ...

  7. websocket、文件上传

    支持情况: 浏览器实现了websocket的浏览器:Chrome Supported in version 4+ Firefox Supported in version 4+ Internet Ex ...

  8. Codeforces 915 E Physical Education Lessons

    题目描述 This year Alex has finished school, and now he is a first-year student of Berland State Univers ...

  9. POJ 3709 K-Anonymous Sequence(斜率优化DP)

    [题目链接] http://poj.org/problem?id=3709 [题目大意] 给出一个长度为n个非严格单调递增数列,每次操作可以使得其中任意一项减一, 问现在使得数列中每项数相同的数的数量 ...

  10. 1.8(java学习笔记)继承与方法的重写

    继承 在java中可以通过继承提高代码的复用率. 例如A继承了B,就可以是 例如,首先有一个类似Person,这个类中有有一些属性和方法,我们再新建一个Student类,其中有一部分属性和方法与Per ...