POJ 1704 Georgia and Bob(阶梯博弈)题解

题意:有一个一维棋盘,有格子标号1,2,3,......有n个棋子放在一些格子上,两人博弈,只能将棋子向左移,不能和其他棋子重叠,也不能跨越其他棋子,不能超越边界,不能走的人输

思路:可以用阶梯博弈来做。

那么先简单讲一下阶梯博弈:

有一个x阶阶梯,每一阶都有一定数量的石头,每次只能把某一阶梯上任意数量(不为0)的石头往下移动一阶,最多只能移动到地面,不能移动的败。这里先手的策略是这样:对奇数阶阶梯的石子进行Nim博弈,异或和为0必败。为什么不用考虑偶数呢?因为如果后手的人把m颗石头从2*n阶移到2*n-1阶,那么先手的可以把m颗石头从2*n-1阶移到2*n-2阶,重复操作直到到0阶即地面,所以我们可以不考虑偶数阶的石子数。所以奇数阶的石子被移到下一阶时,我们可以直接认为这些石头被移除了。那么直接Nim博弈就可以了。

返回这道题,如图,假如我们把棋子A向左移动一定数量,那么我们可以把棋子B向左移动相同数量来保证AB间隔不变,这里就和阶梯博弈时移动偶数阶石子奇数阶石子数量不变是一样的。所以我们从最后一个棋子开始往前分组,两个一组,每一组的间隔就是奇数阶石子个数。讲一下为什么从最后面开始分,因为最后一个间隔不会因为其他间隔的变化而变小,所以必须作为奇数阶石子保持不变(强行这样理解)。

参考:阶梯博弈算法详解(尼姆博弈进阶)

代码:

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
const int maxn = 1000 + 10;
const int seed = 131;
const ll MOD = 1e9 + 7;
const int INF = 0x3f3f3f3f;
using namespace std;
int a[maxn];
int main(){
    int T, n;
    scanf("%d", &T);
    while(T--){
        scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        sort(a + 1, a + n + 1);
        int ans = 0;
        for(int i = n; i > 0; i -= 2){
            ans ^= (a[i] - a[i - 1] - 1);
        }
        if(ans == 0) printf("Bob will win\n");
        else printf("Georgia will win\n");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/KirinSB/p/9678381.html

时间: 2024-07-29 10:38:24

POJ 1704 Georgia and Bob(阶梯博弈)题解的相关文章

POJ 1704 Georgia and Bob(阶梯博弈)

Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11357   Accepted: 3749 Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, number the grids from left to right by 1, 2, 3, ..., and place N c

POJ 1704 Georgia and Bob [阶梯Nim]

题意: 每次可以向左移动一个棋子任意步,不能跨过棋子 很巧妙的转化,把棋子间的空隙看成石子堆 然后裸阶梯Nim #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; typedef long long ll; const int N=1005; inline int re

POJ 1704 Georgia and Bob(阶梯博弈+证明)

POJ 1704 题目链接 关于阶梯博弈有如下定理: 将所有奇数阶梯看作n堆石头,做Nim,将石头从奇数堆移动到偶数堆看作取走石头,同样地,异或值不为0(利己态)时,先手必胜. 定理证明看此博:http://blog.csdn.net/kk303/article/details/6692506 以下是POJ 1704的AC代码: //棋子只能往左走(最左有界线),可以走任意多格(>=1) //而且棋子不能越过在它前面的棋子(它左边的棋子) //每个格最多放一个棋子,说明棋子也不能走到另一个棋子所

poj 1704 Georgia and Bob(阶梯博弈)

Georgia and Bob Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 8656   Accepted: 2751 Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, number the grids from left to right by 1, 2, 3, ...

POJ 1704 Georgia and Bob(nim博弈论)

题目地址:POJ 1704 这个题实在巧妙..居然这样就可以转化成了经典的nim模型. 这题可以从左往右两两配对,如果是奇数个的话,就让最左边的与0配对.然后每当对方移动某一对的前一个,你总可以移动该对的后一个来移动回来.所以这是没有影响的.有影响的只是每一对中间的空格数.这就转化成了((n+1)/2)堆石子的游戏,每一堆的石子个数是每一对点之间的空格数.然后用异或求解. 代码如下: #include <iostream> #include <cstdio> #include &l

POJ1704 Georgia and Bob (阶梯博弈)

Georgia and Bob Time Limit: 1000MS   Memory Limit: 10000KB   64bit IO Format: %I64d & %I64u Submit Status Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, number the grids from left to right by 1, 2,

[原博客] POJ 1704 Georgia and Bob

题目链接题意:如图,Georgia和Bob在玩游戏.一个无限长的棋盘上有N个旗子,第i个棋子的位置可以用Pi表示.现在Georgia先走.每个人每一次可以把一枚棋子向左移动任意个格子,但是不能超越其他棋子,也不能和其他棋子处在同一个格子里.如果轮到某一个人的时候Ta再也不能移动棋子了,就判负.现在每个测试数据给定一种情况,如果Georgia会赢,输出“Georgia will win”,如果Bob会赢,输出“Bob will win”,如果不确定,输出“Not sure”.两个人都知道获胜策略是

poj 1704 Georgia and Bob (nim)

题意: N个棋子,位置分别是p[1]...p[N]. Georgia和Bob轮流,每人每次可选择其中一个棋子向左移动若干个位置(不能超过前一个棋子,不能超出最左边[位置1]且不能不移) Georgia先手,问谁赢. 思路: 将棋子按位置从右到左两个两个作为一对.若棋子总个数是奇数,将第一个棋子和[位置0]作为一对.(可想象位置0放了一个棋子). 情况一:先手若移动某对棋子中的第一个棋子K位,则后手可将该对棋子中的第二个棋子也移动K位.即这种情况不对结果产生影响. 情况二:先手若移动某对棋子中的第

POJ - 1704 Georgia and Bob

Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, number the grids from left to right by 1, 2, 3, -, and place N chessmen on different grids, as shown in the following figure for example: Georgia and