「POJ3537」Crosses and Crosses
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 |
题解
在第I个位置放一个X,即可分为两个子游戏,I-3和n-I-2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n,sg[2001]; int getsg(int x) { if(x<0)return 0; if(sg[x]!=-1)return sg[x]; bool mark[2001]; memset(mark,0,sizeof(mark)); for(int i=1;i<=x;i++) mark[getsg(i-3)^getsg(x-i-2)]=1; for(int i=0;;i++)if(!mark[i])return sg[x]=i; } int main() { memset(sg,-1,sizeof(sg)); scanf("%d",&n); if(getsg(n))puts("1"); else puts("2"); return 0; } |
Subscribe