UVA 10561 - Treblecross(博弈SG函数)

UVA 10561 - Treblecross

题目链接

题意:给定一个串,上面有‘X‘和‘.‘,可以在‘.‘的位置放X,谁先放出3个‘X‘就赢了,求先手必胜的策略

思路:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int N = 205;
int t, out[N], on, len, sg[N];
char str[N];

bool win() {
	for (int i = 0; i < len - 2; i++) {
		if (str[i] == 'X' && str[i + 1] == 'X' && str[i + 2] == 'X')
			return true;
 	}
 	return false;
}

int mex(int x) {
	bool vis[N];
	int i, t;
	if (sg[x] != -1) return sg[x];
	if (x == 0) return sg[x] = 0;
	memset(vis, false, sizeof(vis));
	for (int i = 1; i <= x; i++) {
		int t = mex(max(0, i - 3))^mex(max(0, x - i - 2));
		vis[t] = true;
 	}
 	for (int i = 0; i < N; i++) {
 		if (vis[i]) continue;
   		return sg[x] = i;
	}
}

bool towin() {
	for (int i = 0; i < len; i++) {
		if (str[i] == '.') {
			str[i] = 'X';
			if (win()) {
				str[i] = '.';
   				return false;
			}
			str[i] = '.';
  		}
 	}
 	int ans = 0, num = 0;
 	for (int i = 0; i < len; i++) {
 		if (str[i] == 'X' || (i >= 1 && str[i - 1] == 'X') || (i >= 2 && str[i - 2] == 'X') || (i + 1 < len && str[i + 1] == 'X') || (i + 2 < len && str[i + 2] == 'X')) {
 			ans ^= mex(num);
 			num = 0;
   		}
   		else num++;
  	}
  	ans ^= mex(num);
  	return ans == 0;
}

void solve() {
	on = 0;
	len = strlen(str);
 	for (int i = 0; i < len; i++) {
  		if (str[i] != '.') continue;
  		str[i] = 'X';
  		if (win() || towin())
  			out[on++] = i + 1;
		str[i] = '.';
   	}
}

int main() {
	memset(sg, -1, sizeof(sg));
	scanf("%d", &t);
	while (t--) {
		scanf("%s", str);
		solve();
		if (on == 0) printf("LOSING\n\n");
		else {
			printf("WINNING\n%d", out[0]);
			for (int i = 1; i < on; i++)
				printf(" %d", out[i]);
			printf("\n");
  		}
	}
	return 0;
}

UVA 10561 - Treblecross(博弈SG函数),布布扣,bubuko.com

时间: 2024-10-06 01:20:00

UVA 10561 - Treblecross(博弈SG函数)的相关文章

UVA - 10561 Treblecross (SG定理)

Treblecross is a two player gamewhere the goal is to get three X in a row on a one-dimensional board. At the startof the game all cells in the board is empty. In each turn a player puts a X in an empty cell, and if that results in there beingthree X

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 ge

uva 10561 - Treblecross(Nim)

题目链接:uva 10561 - Treblecross 题目大意:n个格子排成一排,其中一些格子有'X',两个游戏者轮流操作,在格子中放X,如果此时出现连续3个X,则获胜.给出先手是否可以取胜,取胜方案的第一步该怎么走. 解题思路:一个X可以导致左右两个的两个格子都不能再放X,因为如果出现XX...XX.X.X,那么下一个人肯定胜利.所以对于长度为n的格子序列,g(x)=maxg(x?3),g(x?4)...g(2)XORg(n?7). 所以可以预先处理出g数组,然后对于给定的序列,枚举先手下

Treblecross 博弈SG值

Treblecross is a two player game where the goal is to get three X in a row on a one-dimensional board. At the start of the game all cells in the board are empty. In each turn a player puts an X in an empty cell, and if the move results three X next t

hdu 3032(博弈sg函数)

题意:与原来基本的尼姆博弈不同的是,可以将一堆石子分成两堆石子也算一步操作,其它的都是一样的. 分析:由于石子的堆数和每一堆石子的数量都很大,所以肯定不能用搜索去求sg函数,现在我们只能通过找规律的办法求得sg的规律. 通过打表找规律可以得到如下规律:if(x%4==0) sg[x]=x-1; if(x%4==1||x%4==2) sg[x]=x; if(x%4==3) sg[x] = x+1. 打表代码: #include<iostream> #include<cstdio> #

HDOJ 5724 博弈SG函数

链接: http://blog.csdn.net/tc_to_top/article/details/51958964 题意: n行20列的棋盘,对于每行,如果当前棋子右边没棋子,那可以直接放到右边,如果有就跳过放到其后面的第一个空位子,A先操作,最后谁无法操作则输,给定每行棋子状态,问先手是否必胜 题解: 组合博弈问题,直接sg函数,因为列只有20,可以状压搞,枚举每个状态,找到该状态下可行的操作然后标记 代码: 31 int sg[1 << 21]; 32 int vis[21]; 33

(转)博弈 SG函数

此文为以下博客做的摘要: https://blog.csdn.net/strangedbly/article/details/51137432 ---------------------------------------------------------------------------------------- 1.定义P-position和N-positon P表示Previous,N表示Next. 即上一个移动的人有必胜策略的局面是P-position,"先手必败"或&qu

Marbles(博弈SG函数)

Marbles Gym - 101908B Using marbles as a currency didn't go so well in Cubic?nia. In an attempt to make it up to his friends after stealing their marbles, the Emperor decided to invite them to a game night in his palace. Of course, the game uses marb

[hdu-5795]A Simple Nim 博弈 尼姆博弈 SG函数打表找规律

[题目]题目链接 Two players take turns picking candies from n heaps,the player who picks the last one will win the game.On each turn they can pick any number of candies which come from the same heap(picking no candy is not allowed).To make the game more int