首页 > ACM题库 > HDU-杭电 > HDU 3389-Game-博弈论-[解题报告]HOJ
2014
03-23

HDU 3389-Game-博弈论-[解题报告]HOJ

Game

问题描述 :

Bob and Alice are playing a new game. There are n boxes which have been numbered from 1 to n. Each box is either empty or contains several cards. Bob and Alice move the cards in turn. In each turn the corresponding player should choose a non-empty box A and choose another box B that B<A && (A+B)%2=1 && (A+B)%3=0. Then, take an arbitrary number (but not zero) of cards from box A to box B. The last one who can do a legal move wins. Alice is the first player. Please predict who will win the game.

输入:

The first line contains an integer T (T<=100) indicating the number of test cases. The first line of each test case contains an integer n (1<=n<=10000). The second line has n integers which will not be bigger than 100. The i-th integer indicates the number of cards in the i-th box.

输出:

The first line contains an integer T (T<=100) indicating the number of test cases. The first line of each test case contains an integer n (1<=n<=10000). The second line has n integers which will not be bigger than 100. The i-th integer indicates the number of cards in the i-th box.

样例输入:

2
2
1 2
7
1 3 3 2 2 1 2

样例输出:

Case 1: Alice
Case 2: Bob

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3389


题意:1-N带编号的盒子,当编号满足A>B && A非空 && (A + B) % 3 == 0 && (A + B) % 2 == 1则可以从A中取任

石头到B中,谁不能取了谁就输。


分析: 其本质为阶梯博弈。

  

阶梯博弈:博弈在一列阶梯上进行,每个阶梯上放着自然数个点,两个人进行阶梯博弈,每一步则是将一个集体上的若干个点

( >=1 )移到前面去,最后没有点可以移动的人输。


在本题中 1,3,4 的状态不能转移到其他状态; 其他每个状态皆可转移; 且位置特定, 如  2->1 , 5->4, 6->3, 7->2

, 8->1 9->6…..


其本质我们有N级阶梯,现在要在 %3 的余数间转移, 0->0, 1->2, 2->1; 其最后的结果为1, 3, 4; 那么他们的转移

的步数的奇偶性也会确定,我们只要选择步数为奇数的位置做nim博弈就行了;而可以通过打表归纳证明得出模6为0、2、5

的位置移动步数为奇,其余为偶。

#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

int main()
{
    int T,tt=1;
    int n;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        int ans = 0;
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            if(i%6==0 || i%6==2 || i%6==5)
                ans ^= x;
        }
        printf("Case %d: ",tt++);
        if(ans) puts("Alice");
        else puts("Bob");
    }
    return 0;
}


参考:http://blog.csdn.net/ACdreamers/article/details/17028405


  1. 思路二可以用一个长度为k的队列来实现,入队后判断下队尾元素的next指针是否为空,若为空,则出队指针即为所求。

  2. simple, however efficient. A lot of instances it is difficult to get that a??perfect balancea?? among usability and appearance. I must say that youa??ve done a exceptional task with this. Also, the blog masses quite fast for me on Web explore.

  3. 其实国内大部分公司对算法都不够重视。特别是中小型公司老板根本都不懂技术,也不懂什么是算法,从而也不要求程序员懂什么算法,做程序从来不考虑性能问题,只要页面能显示出来就是好程序,这是国内的现状,很无奈。