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函数),布布扣,bubuko.com

时间: 2024-08-01 22:45:14

UVA 11534 - Say Goodbye to Tic-Tac-Toe(博弈sg函数)的相关文章

tic tac toe

井字棋 tic tac toe,布布扣,bubuko.com

uva 11534 - Say Goodbye to Tic-Tac-Toe(Nim和)

题目链接:uva 11534 - Say Goodbye to Tic-Tac-Toe 题目大意:给定一个1*n的个子,每次操作可以选中一个未填过的个子画X或者O,如果该次操作形成了XX或者OO,那么该次操作者视为失败,人为先手,对于给定状态(注意当前状态也算在步数中),问是否可以战胜电脑. 解题思路:对于固定长度,两端的可能有空,X,O,组合情况共有9种,虽然有些情况等价,但是为方便处理,分为9种情况考虑.预先处理出各个长度下9种情况的SG值.然后对于给定状态,枚举位置放X和O,判断新状态的N

amazon.设计1. tic tac toe

//不觉中 已经全力找工作好久好久了.大概有1年半了.身心疲惫,不要放弃.曙光快来了. 1.tic tac toe //http://www.ntu.edu.sg/home/ehchua/programming/java/JavaGame_TicTacToe.html 类似相关棋牌坐标对战游戏 一通百通 /** * Enumerations for the various states of the game */ public enum GameState { // to save as "G

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/principlescomputing-001/wiki/view? page=trees 1.2 CODE无parent域的树 http://www.codeskulptor.org/#poc_tree.py class Tree: """ Recursive definition for trees plus

2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

题面: C. Team Tic Tac Toe Input ?le: standard input Output ?le: standard output Time limit: 1 second Memory limit: 256 megabytes Farmer John owns 26 cows, which by happenstance all have names starting with different letters of the alphabet, so Farmer J

【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-Toe: Players take turns placing characters into empty squares (" "). The first player A always places "X" characters, while the second pl

UVA 10561 - Treblecross(博弈SG函数)

UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了 代码: #include <stdio.h> #include <string.h> #include <algorithm> usi

UVa 10363 - Tic Tac Toe

题目:给你一个井字棋的状态,判断是否合法. 分析:枚举.直接枚举多有情况判断即可. 合法状态有三种情况:(X先下子) 1.X赢,则O不能赢,且X比O多一子: 2.O赢,则X不能赢,且O和X子一样多: 3.没人赢,此时O的子和可能和X一样多,也可能少一个. 说明:简单题(⊙_⊙). #include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include &l

Epic - Tic Tac Toe

N*N matrix is given with input red or black.You can move horizontally, vertically or diagonally. If 3 consecutive samecolor found, that color will get 1 point. So if 4 red are vertically then pointis 2. Find the winner. def tic_tac_toe(board,n) red,