首页 > 代码库 > LightOj 1020 博弈

LightOj 1020 博弈

思路介绍:

       1. 如果首先由Alice取,定义ans[i],如果ans[i]=1表示Alice会取胜,反之Bob取胜。枚举前100项,ans[1]=0,ans[2]=1,ans[i]=!(ans[i-1]&&ans[i-2]);

          可以发现规律:当i为2,3,5,6,8,9....时Alice取胜,所以Alice取胜的条件为:i%3!=1;

      2.如果Bob先取,ans[1]=1,ans[2]=1,ans[i]=!(ans[i-1]&&ans[i-2])

        规律:当i为1,2,4,5,7,8...时Bob取胜,、;所以Bob取胜的条件为:i%3!=0;

 

1020 - A Childhood Game
PDF (English) Statistics Forum
Time Limit: 0.5 second(s) Memory Limit: 32 MB
Alice and Bob are playing a game with marbles; you may have played this game in childhood. The game is playing by alternating turns. In each turn a player can take exactly one or two marbles.

Both Alice and Bob know the number of marbles initially. Now the game can be started by any one. But the winning condition depends on the player who starts it. If Alice starts first, then the player who takes the last marble looses the game. If Bob starts first, then the player who takes the last marble wins the game.

Now you are given the initial number of marbles and the name of the player who starts first. Then you have to find the winner of the game if both of them play optimally.

Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case contains an integer n (1 ≤ n < 231) and the name of the player who starts first.

Output
For each case, print the case number and the name of the winning player.

Sample Input
Output for Sample Input
3
1 Alice
2 Alice
3 Bob
Case 1: Bob
Case 2: Alice
Case 3: Alice


PROBLEM SETTER: JANE ALAM JAN

<span style="color:#6600cc;">/**************************************
     author   : Grant Yuan
     time     : 2014/8/20 13:38
     algorithm: 博弈
     source   : LightOj 1020
***************************************/
#include<bits/stdc++.h>
#define Alice "Alice"
#define Bob "Bob"

using namespace std;
char s[6];
int n,t;
int main()
{
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        memset(s,0,sizeof(s));
        scanf("%d%s",&n,s);
        printf("Case %d: ",i);
        if(strcmp(s,Alice)==0){
            if(n%3==1){
                printf("Bob\n");
            }
            else{
                printf("Alice\n");
            }
        }
        else{
            if(n%3==0){
                printf("Alice\n");
            }
            else{
                printf("Bob\n");
            }
        }
    }
    /*ans[1]=false;ans[2]=true;
    for(int i=3;i<=100;i++)
    {
        ans[i]=true;
        if(ans[i-1]&&ans[i-2])
            ans[i]=false;
    }
    for(int i=1;i<=100;i++)
    {
        printf("%d ",i);
        if(ans[i])
            printf("1\n");
        else
            printf("0\n");
    }*/
    return 0;
}
</span>