bnu Game 博弈。
Game
Input
The first line of each test case contains an integer n (1<=n<=1000), and n lines follow. Each line contains n integers 0 or 1, which means there exist a stone or not (1 means exist).
Output
Sample Input
3
0 0 0
0 0 0
1 0 0
4
0 0 0 1
1 0 1 0
1 1 1 1
1 0 1 0
Sample Output
Case 2: Alice
这道题,是一道博弈题。
每一行是可以单独考虑的,这个是很好理解的。
奇数和偶数行也是可以单独考虑的。推一推。
“1”的存在,用来分割他们进行讨论。
题目的转化为 n 堆石头子,每次从每一堆取出2个连续堆的方案。
这里就简单了。
- 1 #include<iostream>
- 2 #include<cstdio>
- 3 #include<cstring>
- 4 #include<cstdlib>
- 5 using namespace std;
- 6
- 7 int SG[1003];
- 8 bool use[1003];
- 9 void prepare()
- 10 {
- 11 int i,j;
- 12 SG[0]=0;SG[1]=0;
- 13 for(i=2;i<=1000;i++)
- 14 {
- 15 memset(use,false,sizeof(use));
- 16 for(j=1;j<i;j++)
- 17 {
- 18 use[ (SG[j-1] ^ SG[i-j-1]) ]=true;
- 19 }
- 20 for(j=0;;j++)
- 21 if(use[j]==false)
- 22 {
- 23 SG[i]=j;
- 24 break;
- 25 }
- 26 }
- 27 }
- 28 int main()
- 29 {
- 30 int T,n,ans[2],t;
- 31 int i,j,x,XOR;
- 32 prepare();
- 33 scanf("%d",&T);
- 34 for(t=1;t<=T;t++)
- 35 {
- 36 scanf("%d",&n);
- 37 XOR=0;
- 38 for(i=1;i<=n;i++)
- 39 {
- 40 ans[0]=0;ans[1]=0;
- 41 for(j=1;j<=n;j++)
- 42 {
- 43 scanf("%d",&x);
- 44 if(x==0)
- 45 ans[j%2]++;
- 46 else
- 47 {
- 48 XOR=XOR^SG[ans[j%2]];
- 49 ans[j%2]=0;
- 50 }
- 51 }
- 52 XOR=XOR^SG[ans[0]];
- 53 XOR=XOR^SG[ans[1]];
- 54 }
- 55 printf("Case %d: ",t);
- 56 if(XOR==0)
- 57 printf("Bob\n");
- 58 else printf("Alice\n");
- 59 }
- 60 return 0;
- 61 }
SG[ ] 使用map。
- 1 #include<iostream>
- 2 #include<cstdio>
- 3 #include<cstring>
- 4 #include<cstdlib>
- 5 #include<map>
- 6 using namespace std;
- 7
- 8 int SG[1003];
- 9 map<int,int>Q;
- 10 void prepare()
- 11 {
- 12 int i,j,k,s;
- 13 SG[0]=0;SG[1]=0;
- 14 for(i=2;i<=1000;i++)
- 15 {
- 16 while(!Q.empty())
- 17 {
- 18 Q.clear();
- 19 }
- 20 for(s=0,j=1;j<i;j++)
- 21 {
- 22 k=(SG[j-1] ^ SG[i-j-1]);
- 23 Q[k]=s++;
- 24 }
- 25 for(j=0;;j++)
- 26 if(Q.find(j)==Q.end())
- 27 {
- 28 SG[i]=j;
- 29 break;
- 30 }
- 31 }
- 32 }
- 33 int main()
- 34 {
- 35 int T,n,ans[2],t;
- 36 int i,j,x,XOR;
- 37 prepare();
- 38 scanf("%d",&T);
- 39 for(t=1;t<=T;t++)
- 40 {
- 41 scanf("%d",&n);
- 42 XOR=0;
- 43 for(i=1;i<=n;i++)
- 44 {
- 45 ans[0]=0;ans[1]=0;
- 46 for(j=1;j<=n;j++)
- 47 {
- 48 scanf("%d",&x);
- 49 if(x==0)
- 50 ans[j%2]++;
- 51 else
- 52 {
- 53 XOR=XOR^SG[ans[j%2]];
- 54 ans[j%2]=0;
- 55 }
- 56 }
- 57 XOR=XOR^SG[ans[0]];
- 58 XOR=XOR^SG[ans[1]];
- 59 }
- 60 printf("Case %d: ",t);
- 61 if(XOR==0)
- 62 printf("Bob\n");
- 63 else printf("Alice\n");
- 64 }
- 65 return 0;
- 66 }