LightOJ 1229 Tablecross
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 to each other, that player wins.
Given the current state of the game, you are to determine if the current player to move can win the game assuming both players play optimally.
Consider the game where the board size is 5 cells. If the first player puts an X at position three (in the middle) so the state becomes ..X.., he will win the game as no matter where the other player puts his X, the first player can get three X in a row. If, on the other hand, the first player puts the X in any other position, the second player will win the game by putting the X in the opposite corner (for instance, after the second players move the state might be .X..X). This will force the first player to put an X in a position so the second player wins in the next move.
Input
Input starts with an integer T (≤ 200), denoting the number of test cases.
Each case starts with a line containing a string denoting the current status of the game. The string will only contain the characters '.' and 'X'. The length of the string (the size of the board) will be between 3 and 200 characters, inclusive. No state will contain three X in a row.
Output
For each case, print the case number and the positions on the board, where the player to move may put an X and win the game. The positions should be separated by a single space, and be in increasing order. The leftmost position on the board is 1. If there is no such position print 0.
Sample Input
4
.....
X.....X..X.......X....X..X
.X.X...X
..................
Sample Output
Case 1: 3
Case 2: 0
Case 3: 3
Case 4: 5 6 13 14
题解:题目意思就是给你一行字符串由 '.'和'X'组成,然后两个人交替将一个‘.’
变成'X'.如果某个人先形成连续的3个‘X’,则这个人就取得胜利。问先手必胜的位置是否存在,
如果存在,有多少个,并依次输出其位置;这题肯定是枚举每一个位置,判断是否可以胜利,
对于SG[x]表示长度为x的‘.’区间的SG值,然后对于每一个位置(不是'X'的位置),判断其由
‘.’变成'X'之后是否可以形成连续3个'X'的必胜状态,是否会形成.XX或XX.或X.X的必败状态;如果都不是
再去枚举每一个区间的SG值,再将其异或ans,就得到这一个位置的SG值,为0必胜,不为零必败;
参考代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
typedef long long ll;
#define clr(a,val) memset(a,val,sizeof(a))
const int maxn=;
int T,SG[maxn],len;
vector<int> v;
char s[maxn],s1[maxn];
int getSG(int m)
{
if(m<) return ;
if(SG[m]!=-) return SG[m];
bool vis[maxn];clr(vis,);
for(int i=;i<=m;++i) vis[getSG(i-)^getSG(m-i-)]=;
int t=;
while(vis[t]) ++t;
return SG[m]=t;
} bool check(int x)
{
strcpy(s1,s);
if(s1[x]=='X') return ;
s1[x]='X';
for(int i=;i<len-;++i) {if(s1[i]=='X'&&s1[i+]=='X'&&s1[i+]=='X') return ;}
for(int i=;i<len-;++i) {if(s1[i]=='X'&&s1[i+]=='X') return ;}
for(int i=;i<len-;++i) {if(s1[i]=='X'&&s1[i+]=='X') return ;}
int j=-,f=,ans=;
for(int i=;i<len;++i)
{
if(s1[i]=='X')
{
if(f) ans^=getSG(i-j-);
else ans^=getSG(i-j-),f=;
j=i;
}
}
ans^=getSG(len-j-);
return ans==;
} int main()
{
scanf("%d",&T);
memset(SG,-,sizeof SG);
for(int cas=;cas<=T;++cas)
{
scanf("%s",s);
v.clear();
len=strlen(s);
for(int i=;i<len;++i)
{
if(check(i)) v.push_back(i+);
}
printf("Case %d:",cas);
if(v.size())
{
for(int i=;i<v.size();++i) printf(" %d",v[i]);
puts("");
}
else printf(" 0\n");
} return ;
}
最新文章
- 设计模式(十)组合模式(Composite Pattern)
- TaintDroid剖析之Native方法级污点跟踪分析
- Highcharts使用指南
- HDU2255-奔小康赚大钱-二分图最大权值匹配-KM算法
- 常用浏览器user_agent大全
- AX3空Invoice明细问题
- php get_ini 和 get_cfg_var 的区别
- 引用 - PHP手册笔记
- Java String.indexOf() 函数用法小结
- Vultr优惠码20美元享受20GB SSD和2T流量
- redis源码分析之事务Transaction(上)
- 机器学习基石笔记:03 Types of Learning
- sencha touch 选择器
- 『PyTorch』第五弹_深入理解Tensor对象_下:从内存看Tensor
- Java实现循环链表
- postgresql copy的使用方式
- 4个设计绝招教你减少PCB板电磁干扰
- OKEX API
- Nginx Configuration 免费HTTPS加密证书
- 小米笔记本pro CPU GPU 做科学计算的算力对比