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( ...
随机推荐
- 【集合类型的并发】Collections.synchronizedList
摘要: 详细的解析:Collections.synchronizedList :关注要点,为什么在有synchroniezed方法的同时会出现 Collections.synchronizedList ...
- RTP 学习
1. RTP提供抖动补偿和数据无序到达检测的机制 2. RTP 本身并没有提供按时发送机制或其它服务质量(QoS)保证,它依赖于底层服务去实现这一过程. RTP标准定义了两个子协议,RTP和RTCP. ...
- opencv图像的旋转
#include"stdafx.h"#include"opencv2/opencv.hpp" using namespace cv;// clockwise 为 ...
- ASP.NET Core 2.2 基础知识(十七) SignalR 一个极其简陋的聊天室
这是一个极其简陋的聊天室! 这个例子只是在官方的例子上加了 Group 的用法而已,主要是官方给的 Group 的例子就两行代码,看不出效果. 第一步:修改 chat.js "use str ...
- JavaScript函数中的参数(arguments)
arguments argument是JavaScript中的一个关键字,用于指向调用者传入的所有参数. function example(x){ alert(x); alert(arguments. ...
- 【可持久化Trie】bzoj3261 最大异或和
对原序列取前缀异或值,变成pre[1...N],然后询问等价于求max{a[N]^x^pre[i]}(l-1<=i<=r-1). #include<cstdio> #defin ...
- Mybatis更新用户
xml配置 <!--更新用户 --> <update id="updateUserById" parameterType="com.itheima.my ...
- 理解面向对象编程---C#控制台实现52张扑克牌的分法
52张牌随机分给4个玩家,要求每个玩家的牌用一个一维数组表示. 我们采用模拟大法.初始化一副扑克牌,洗牌,发牌. using System; using System.Collections.Gene ...
- python - ImportError: No module named pywintypes
must restart the python shell to avoid this issue after pywin32 installed
- IP windows相关
nbtstat: 假设 我们 通过net view 获取了 局域网内一些计算机名 如上标红的计算机名称 如何 才能获取计算机的ip呢? 接下来使用 nbtstat -a 列出远程机器的名称表: ...