• bnu Game 博弈。


     Game

    10000ms
    10000ms
    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
    ");
    58         else printf("Alice
    ");
    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
    ");
    63         else printf("Alice
    ");
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    day10 Java学习(开发工具.Eclipse)
    day9 Java学习(面向对象Package关键字)
    day8 Java学习(面向对象.多态&接口)
    day7 Java学习(面向对象.继承)
    day5 Java学习(面向对象)
    day4 Java学习(数组)
    day3 Java学习(循环结构)
    day2 Java学习(位运算符)
    2019-05-26 java学习日记
    2019-05-25 java学习日记
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3434848.html
Copyright © 2020-2023  润新知