bnu Game 博弈。

 Game

Time Limit:  10000ms
Case Time Limit:  10000ms
Memory Limit:  65536KB
 
64-bit integer IO format:   %lld       Java class name:   Main
Alice and Bob invented a new game again, as they usually did.
The rule of the new game is quite simple. There is an n*n matrix and at the very beginning of the game, some of ceil contain stone, and others contain nothing. Then they take turns (Alice first) to play the game.
Each time one person should choose two empty ceils which are on the same line, and the number of their columns’ difference is exactly 2(which means the absolute value of difference is 2). And then the people should put stones on each of the two ceil. The game ends when one people cannot do the operation above and thus lose the game.
Here's the problem: Who will win the game if both use the best strategy? Find it out quickly, before they get bored of the game again!
 
 

Input

The first line contains a single integer T, indicating the number of test cases.
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

For each test case, output the case number first, then “Alice” if Alice will win otherwise output “Bob”.
 

Sample Input

2
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 1: Bob
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 }

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/tom987690183/p/3434848.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值