转自http://blog.csdn.net/kk303/article/details/6692506
今天在POJ做了一道博弈题..进而了解到了阶梯博弈...下面阐述一下我对于阶梯博弈的理解..
首先是对阶梯博弈的阐述...博弈在一列阶梯上进行...每个阶梯上放着自然数个点..两个人进行阶梯博弈...每一步则是将一个集体上的若干个点( >=1 )移到前面去..最后没有点可以移动的人输..
今天在POJ做了一道博弈题..进而了解到了阶梯博弈...下面阐述一下我对于阶梯博弈的理解..
如这就是一个阶梯博弈的初始状态 2 1 3 2 4 ... 只能把后面的点往前面放...如何来分析这个问题呢...其实阶梯博弈经过转换
可以变为Nim..把所有奇数阶梯看成N堆石子..做nim..把石子从奇数堆移动到偶数堆可以理解为拿走石子..就相当于几个奇数堆的石子在做Nim..( 如所给样例..2^3^4=5 不为零所以先手必败)为什么可以这样来转化?
一道例题:
Description
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.
Input
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.
Output
For each test case, print the case number and the winner's name in a single line. Follow the format of the sample output.
Sample Input
2 2 1 2 7 1 3 3 2 2 1 2
Sample Output
Case 1: Alice Case 2: Bob
代码:
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<climits>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<ctime>
#define MAXN 10010
typedef long long LL;
using namespace std;
int main(void)
{
#ifdef LOCAL
freopen("in.txt","rb",stdin);
//freopen("out.txt","wb",stdout);
#endif
int t;
scanf("%d",&t);
for(int tt=1;tt<=t;tt++)
{
int n;
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;
}
思路:1,3,4这三个盒子是不可移动的,移动到这三个盒子需要步数为奇数的试做位于奇数阶梯上。