poj 3537 Crosses and Crosses 博弈论

思路:每次画X之后都会形成2个子游戏,即i-3和n-i-2.

代码如下:

 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<vector>
 6 #include<cstring>
 7 using namespace std;
 8 int sg[2002];
 9 int getsg(int n)
10 {
11     if(n<=0) return 0;
12     if(sg[n]>=0) return sg[n];
13     bool vis[2002]={0};
14     for(int i=1;i<=n;i++){
15         vis[getsg(n-i-2)^getsg(i-3)]=1;
16     }
17     int i=0;
18     while(vis[i]) i++;
19     return sg[n]=i;
20 }
21 int main()
22 {
23     int n;
24     memset(sg,-1,sizeof(sg));
25     while(scanf("%d",&n)!=EOF){
26         puts(getsg(n)?"1":"2");
27     }
28     return 0;
29 }
View Code

 

 

posted @ 2013-09-12 20:11  _随心所欲_  阅读(179)  评论(0编辑  收藏  举报