2016多校赛 1003(博弈)

Life Winner Bo

Time Limit: 2000/1000 MS (Java/Others)

Problem Description
Bo is a “Life Winner”.He likes playing chessboard games with his girlfriend G.

The size of the chessboard is N×M.The top left corner is numbered(1,1) and the lower right corner is numberd (N,M).

For each game,Bo and G take turns moving a chesspiece(Bo first).At first,the chesspiece is located at (1,1).And the winner is the person who first moves the chesspiece to (N,M).At one point,if the chess can’t be moved and it isn’t located at (N,M),they end in a draw.

In general,the chesspiece can only be moved right or down.Formally,suppose it is located at (x,y),it can be moved to the next point (x′,y′) only if x′≥x and y′≥y.Also it can’t be moved to the outside of chessboard.

Besides,There are four kinds of chess(They have movement rules respectively).

1.king.

2.rook(castle).

3.knight.

4.queen.

(The movement rule is as same as the chess.)

For each type of chess,you should find out that who will win the game if they both play in an optimal strategy.

Print the winner’s name(“B” or “G”) or “D” if nobody wins the game.

Input
In the first line,there is a number T as a case number.

In the next T lines,there are three numbers type,N and M.

“type” means the kind of the chess.

T≤1000,2≤N,M≤1000,1≤type≤4

Output
For each question,print the answer.

Sample Input

4
1 5 5
2 5 5
3 5 5
4 5 5

Sample Output

G
G
D
B

题目大意:

有一个n*m的棋盘,如果能从左上角走到右下角就算赢,B为先手,G为后手,有四种棋子,分别是1:国王 2:车 3:马 4:皇后,(其中皇后我当时推不出来,原来要用到威佐夫博弈)

题目分析:

1003 Life Winner Bo
//这是官方题解:
我们依次分析每一种棋子。

①王。

首先注意一个3*3的棋盘,开始在(1,1),问走到(3,3)谁有必胜策略。

穷举所有情况,容易发现这是后手赢。

对于NN和MM更大的情况,我们把横坐标每隔3、纵坐标每隔3的点都画出来,这些点都是符合后手胜的。
(因为无论先手怎么移动,后手都能重新移动到这些格子,直到到了终点)

如果初始点不在这些点上,就必然是先手胜。因为先手可以立刻移动到上述的点。

②车。

注意到,如果目前的位置距离终点的xx和yy坐标差相等,一定是后手胜。
(因为先手只能向下或者向右走一段路;无论他往哪里走,后手往另一维走相同的步数,依然保持这一样一种状态。)

反之,先手必然能走到一处相等的位置,转化为上述问题,所以一定是先手胜。

③马。

同样还是画图可以得到规律。

在大多数情况下都是平局。在模3域下,某些地方会存在先后手赢。

④皇后。

画画图后,我们可以将问题转化为:

“有两堆石子,每次可以在一堆里取任意(非空)颗(相当于是车的走法),或者在两堆里取相同(非空)颗(相当于是象的走法),取到最后一颗石子的人获胜,问先后手谁有必胜策略。”

此题中N\leq 1000N≤1000,可以直接用DP的方法解决。
设f[x][y]为横坐标距离终点x步,纵坐标距离终点y步时,必胜的是先手还是后手。

直接转移的话,可以枚举先手的下一步决策进行转移,这样是O(N^3)O(N
​3
​​ )的。

注意到转移只是一行、一列或者斜着一列,这些都可以通过前缀和,做到最终O(N^2)O(N
​2
​​ )。

其实对于更大的NN也是可以做的。

由于叙述起来比较麻烦,具体的结论和证明可以参见:
威佐夫博弈

那么问题来了,给定一组(a,b)如何判断它是不是T态,显然只要找到它是第几个T态即可,已知公式a[i] = (int)(i*(sqrt(5)+1)/2),b[i] = a[i]+i,那么就可以通过i = a[i]*(sqrt(5)-1)/2)求出i的值,当然有可能出现精度缺失,所以有可
能算出的i值比实际i值小1,接下来判断a[i]是否满足a[i] = (int)(i*(sqrt(5)+1)/2),如果满足,那么看b[i]是否满足b[i]
 = a[i]+i,如果全部满足说明为T态,否则为S态

所以这里皇后走法等价于威佐夫博弈,
怎么做呢?

g1=(sqrt(5)+1)/2;
g2=(sqrt(5)-1)/2;(即g1-1

对于(n,m)(不妨假设m>=n)

n=[i*g1];(i=0,1,2,3,4...)
m=n+i;

我们可以先限定住i,用i=int(n*g2)因为有精度误差,所以也可能等于i=int(n *g2)+1
然后验证m是否等于n+i

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+3;
const double gold1=(sqrt(5.)+1)/2.;
const double gold2=gold1-1;
bool vis[maxn];
int sg[maxn],n,m,type,t;

void gett3(int n,int m)
{
   if((m==1&&n==1)||((m-1)%3==0&&(n-1)%3==0))puts("G");
   else
        if((n%3==0&&(m+1)%3==0)||(m%3==0&&(n+1)%3==0))puts("B");
   else
    puts("D");
}

void gett4(int n,int m)
{
    if(n>m)swap(n,m);//n<=m,(n,m)
    int i=n*gold2;
    if(n==int(gold1*i)&&m==n+i||n==int(gold1*(i+1))&&m==n+i+1)puts("B");
    else
        puts("G");
}

int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&type,&n,&m);
        if(type==1)
        {
            if((m&1)&&(n&1))puts("G");
            else
                puts("B");
        }
        else
            if(type==2)
        {
            if(m==n)puts("G");
            else
                puts("B");
        }
        else
            if(type==3)
        {
            gett3(n,m);
        }
        else
            if(type==4)
        {
            gett4(n,m);
        }
    }
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值