POJ 3537 Crosses and Crosses
Crosses and Crosses
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 3514 | Accepted: 1372 | |
Case Time Limit: 2000MS |
Description
The game of Crosses and Crosses is played on the field of 1 × n cells. Two players make moves in turn. Each move the player selects any free cell on the field and puts a cross ‘×’ to it. If after the player’s move there are three crosses in a row, he wins.
You are given n. Find out who wins if both players play optimally.
Input
Input file contains one integer number n (3 ≤ n ≤ 2000).
Output
Output ‘1’ if the first player wins, or ‘2’ if the second player does.
Sample Input
#1 | 3 |
---|---|
#2 | 6 |
Sample Output
#1 | 1 |
---|---|
#2 | 2 |
Source
Northeastern Europe 2007, Northern Subregion
分析:
显然,如果一个棋子放在了棋盘上,那么这个棋子的左边两个位置和右边两个位置都不能再放棋子,问题就转化为了一个棋子的左右两边的四个位置不能放,谁不能放谁输...
对于一个$1*n$的棋盘我们把其看成一个规模为$n$的游戏,这个游戏的后继状态我们可以$O(N)$的枚举计算,所以我们可以算出所有游戏的$SG$函数...
代码:
#include<algorithm> #include<iostream> #include<cstring> #include<cstdio> //by NeighThorn using namespace std; const int maxn=2000+5; int n,f[maxn]; inline int sg(int x){ if(f[x]!=-1) return f[x]; bool vis[maxn]; memset(vis,0,sizeof(vis)); for(int i=1;i<=x;i++) vis[sg(max(0,i-3))^sg(max(x-i-2,0))]=1; for(int i=0;;i++) if(!vis[i]) return f[x]=i; } signed main(void){ scanf("%d",&n); memset(f,-1,sizeof(f));f[0]=0; puts(sg(n)?"1":"2"); return 0; }
By NeighThorn