博弈——Alice and Bob

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/LSC_333/article/details/77833681

Alice and Bob are interested in playing games. One day, they invent a game which has rules below: 
1. Firstly, Alice and Bob choose some random positive integers, and here we describe them as n, d  1, d  2,..., d  m
2. Then they begin to write numbers alternately. At the first step, Alice has to write a “0”, here we let s  1 = 0 ; Then, at the second step, Bob has to write a number s  2 which satisfies the condition that s  2 = s  1 + d  k and s  2 <= n, 1<= k <= m ; From the third step, the person who has to write a number in this step must write a number s  i which satisfies the condition that s  i = s  i-1 + d  k or s  i = s  i-1- d  k , and s  i-2 < s  i <= n, 1 <= k <= m, i >= 3 at the same time. 
3. The person who can’t write a legal number at his own step firstly lose the game.
Here’s the question, please tell me who will win the game if both Alice and Bob play the game optimally.
InputAt the beginning, an integer T indicating the total number of test cases. 
Every test case begins with two integers n and m, which are described before. And on the next line are m positive integers d  1, d  2,..., d  m
T <= 100; 
1 <= n <= 10  6
1 <= m <= 100; 
1 <= d  k <= 10  6, 1 <= k <= m. 
OutputFor every test case, print “Case #k: ” firstly, where k indicates the index of the test case and begins from 1. Then if Alice will win the game, print “Alice”, otherwise “Bob”. Sample Input
2
7 1
3
7 2
2 6
Sample Output
Case #1: Alice
Case #2: Bob

题意:

给一个数n和m个数d[m],现在A和B轮流进行操作,不能进行操作的人为输,规则如下:

1.第一步A写一个0,设s[1]=0,第二步B令s[2]=s[1]+d[i]

2.两人轮流从d数组中任选一个数d[k],另s[i]=s[i-1]+d[k]或者s[i]=s[i-1]-d[k];

3.s[i]>s[i-2]并且s[i]<=n

思路;

从最终状态开始看,假设min是d中最小的数,如果s[i]+min>n,那么游戏结束,所以游戏的最后一步一定是s[i]=s[i-1]+min之后使得s[i]+min>n,至于为什么不能s[i]-d[i](违反了第三规则),所以对于任何一个状态如果我选择的不是min那么对手总可以减去min来使自己进行依一次操作,但是如果我选min,对手就只能往上加。所以对于两个人的最好的策略就是每次都加一个min,看谁先不能加即可

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <stack>
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;

int main()
{
    int t;
    scanf("%d", &t);
    for(int cas=1; cas<=t; cas++)
    {
        int n, m;
        scanf("%d%d", &n, &m);
        int minn=INF;
        int x;
        for(int i=0; i<m; i++)
        {
            scanf("%d", &x);
            minn=min(x, minn);
        }
        int k=n/minn;
        if(k&1)
            printf("Case #%d: Bob\n" , cas);
        else
            printf("Case #%d: Alice\n", cas);
    }
    return 0;
}






猜你喜欢

转载自blog.csdn.net/LSC_333/article/details/77833681