uva10561 - Treblecross
2015-08-08 10:59
295 查看
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 is empty. In each turn a player puts a `X' in an empty
cell, and if that results in there being three `X' next to each other, that player wins.
Given the current state of the game, you are to determine if the player to move can win the game
assuming both players play perfectly. If so, you should also print all moves that will eventually lead to
a win.
Consider the game where the board size is 5 cells. If the rst player puts a `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 rst player can get three `X' in a row. If, on the other hand, the rst 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 player moves the state might be `.X..X'). This will force the rst player
to put an `X' in a position so the second player wins in the next move.
Input
The input begins with an integer N (N < 100), the number of states that will follow. Each state is
represented by a string on a line by itself. 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, rst output `WINNING' or `LOSING' depending on if the player to move will win or lose the
game. On the next line, output in increasing order all positions on the board where the player to move
may put an X and win the game. The positions should be separated by a blank, and be in increasing
order. The leftmost position on the board is 1.
Sample Input
4
.....
X.....X..X.............X....X..X
.X.X...X
...............................................
Sample Output
WINNING
3
LOSING
WINNING
3
WINNING
1 12 15 17 20 24 28 31 33 36 47
白书上的原题,注意此题细节非常多。
code:
At the start of 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 being three `X' next to each other, that player wins.
Given the current state of the game, you are to determine if the player to move can win the game
assuming both players play perfectly. If so, you should also print all moves that will eventually lead to
a win.
Consider the game where the board size is 5 cells. If the rst player puts a `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 rst player can get three `X' in a row. If, on the other hand, the rst 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 player moves the state might be `.X..X'). This will force the rst player
to put an `X' in a position so the second player wins in the next move.
Input
The input begins with an integer N (N < 100), the number of states that will follow. Each state is
represented by a string on a line by itself. 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, rst output `WINNING' or `LOSING' depending on if the player to move will win or lose the
game. On the next line, output in increasing order all positions on the board where the player to move
may put an X and win the game. The positions should be separated by a blank, and be in increasing
order. The leftmost position on the board is 1.
Sample Input
4
.....
X.....X..X.............X....X..X
.X.X...X
...............................................
Sample Output
WINNING
3
LOSING
WINNING
3
WINNING
1 12 15 17 20 24 28 31 33 36 47
白书上的原题,注意此题细节非常多。
code:
#include<cstdio> #include<iostream> #include<cmath> #include<cstring> #include<algorithm> #define maxn 205 using namespace std; char ch,s[maxn]; int ti,n,sg[maxn],cnt,l,idx,tmp; bool ok,bo[maxn*maxn],exist[maxn],flag,first; struct DATA{ int l,r,siz; }list[maxn]; void read(int &x){ for (ok=0,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=1; for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar()); if (ok) x=-x; } void calc(int k){ for (int i=1,t1,t2;i<=k;i++){ t1=(i-2)-1,t2=k-(i+2); if (t1>0&&sg[t1]==-1) calc(t1); if (t2>0&&sg[t2]==-1) calc(t2); } memset(bo,0,sizeof(bo)); for (int i=1,t1,t2,t;i<=k;i++){ t=0,t1=(i-2)-1,t2=k-(i+2); if (t1>0) t^=sg[t1]; if (t2>0) t^=sg[t2]; bo[t]=1; } for (int i=0;;i++) if (!bo[i]){sg[k]=i;break;} } void prepare(){ memset(sg,-1,sizeof(sg)); for (int i=200;i>=1;i--) if (sg[i]==-1) calc(i); } void write(int x){ if (first) first=0; else putchar(' '); printf("%d",x); } int main(){ prepare(); for (read(ti);ti;ti--){ memset(s,0,sizeof(s)); scanf("%s",s+1); n=strlen(s+1),flag=0,idx=cnt=tmp=0,l=(s[1]=='X'?0:1); memset(list,0,sizeof(list)); for (int i=1;i<=n;i++) if (s[i]=='X'){ if (s[i+1]=='X'||s[i+2]=='X'){flag=1;break;} if ((i-2)-l>0) list[++idx]=(DATA){l,i-3,i-2-l}; l=i+3; } if (l<=n) list[++idx]=(DATA){l,n,n-l+1}; if (flag){ puts("WINNING"); memset(exist,0,sizeof(exist)); first=1; for (int i=1;i<=n;i++) if (s[i]=='X'){ if (s[i+1]=='X'){ if (i-1>=1&&!exist[i-1]) write(i-1),exist[i-1]=1; if (i+2<=n&&!exist[i+2]) write(i+2),exist[i+2]=1; } if (s[i+2]=='X'){ if (i+1<=n&&!exist[i+1]) write(i+1),exist[i+1]=1; } } puts(""); continue; } for (int i=1;i<=idx;i++) tmp^=sg[list[i].siz]; if (tmp){ puts("WINNING"); first=1; for (int i=1,t;i<=idx;i++){ t=sg[list[i].siz]; for (int j=list[i].l,t1,t2,t3;j<=list[i].r;j++){ t1=(j-2)-list[i].l,t2=list[i].r-(j+2),t3=0; if (t1) t3^=sg[t1]; if (t2) t3^=sg[t2]; if (!(tmp^t^t3)) write(j); } } puts(""); continue; } else puts("LOSING"),puts(""); } return 0; }
相关文章推荐
- win10电源里没有快速启动这个功能怎么办
- fread函数和read函数的区别
- 九度oj 1451
- 最好用的下拉刷新
- HUD 3911 Black And White 线段树 区间更新 + 区间合并
- uva 11584 动态规划
- cocos2dx-屏幕设置
- scala 中配置文件之 config
- Check the difficulty of problems - poj 2151 (概率+DP)
- 算法导论--矩阵链相乘
- 从服务器读取一张图片
- 牛客网(搜狗2015 C++工程师笔试题)
- ThreadHandle And CloseHandel
- Java基础-IO-其它流(笔记)
- [贪心]poj3045 Cow Acrobats
- 如何调试Javascript代码
- Linux 前后台程序
- 南邮 OJ 2069 赵信的往事
- 1426POJ
- 求最长回文字符串