• URAL2104. Game with a Strip(博弈)


    There is a strip 1 × n with two sides. Each square of the strip (their total amount is 2nn squares on each side) is painted in one of two colors (let’s call them A and B). Alice and Bob play a game. Alice makes the first turn. On each turn, a player can bend the strip in half with the fold passing on the divisions of the squares (i.e. the turn is possible only if the strip has an even length). Bending the strip can be done either inwards or outwards. If the strip has become completely one color after the next turn, then the winner is the player whose color is it (A refers to Alice, B to Bob). If the current player has no legal moves, but no one has won, the game ends with a draw.
    Who will win if both players play optimally? This means that each player tries to win; if it is not possible, to achieve a draw.

    Input

    The first line contains an integer n that is the length of the strip (1 ≤ n ≤ 5 · 105).
    The next two lines contain two strings of letters “A” and “B” with lengths n, describing two sides of the strip. The letters that are under each other, correspond to the different sides of the same square.

    Output

    If Alice wins, output “Alice”. If Bob wins, output “Bob”. If the game ends with a draw, output “Draw”.

    Samples

    inputoutput
    4
    BBAA
    BABB
    
    Bob
    
    3
    AAA
    BBB
    
    Draw
    
    2
    AA
    BB
    
    Alice
    

    Notes

    In the first example, Alice starts the game with the strip BBAA/BABB. After her turn she can get the strip BB/AA or BB/AB. In both cases, Bob can win by getting the strip B/B.
    In the second example, Alice can’t make even the first turn, so the result is a draw.
    In the third example, Alice wins by the first move, getting the stripe A/A from the strip AA/BB.
    Problem Author: Nikita Sivukhin
    Problem Source: Ural FU Junior Championship 2016

    题意:有初始长度为2*N的字符串,上面只有‘A’或者‘B’,每次可以沿对角线翻折,如果翻折后全部为‘A’,则Alice胜利;全部为‘B’则Bob胜利。如果翻折前字符串长度不为偶数则为平局。

    思路:翻折等效于覆盖:即[1,2N]翻折后本来应该是[N,1]或者[2*N,N+1];但是我们实际上不能模拟翻转操作(复杂度有点高,虽然好像NlogN也可以过)。这里,我们考虑翻折操作等效于覆盖操作后为[1,N]或者[N+1,2*N]。

                1,判定一个区间是否颜色全部为‘A’,我们用前缀和判定,如果suma[R]-sum[L-1]==R-L+1,则全为‘A’;  ‘B’同理。

                2,然后对于当前区间,我们考虑两种子情况:两种子情况都不利,则不利;有一个有利,则有利。 (博弈的思想)

    #include<cstdio>
    #include<cstdlib>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int maxn=2000000;
    char c[maxn]; int sum1[maxn],sum2[maxn];
    int judge(int L,int R,int opt)
    {     
        if((R-L+1)%2!=0) return 3;
        if(sum1[R]-sum1[L-1]==R-L+1) return 1; //全A 
        if(sum2[R]-sum2[L-1]==R-L+1) return 2; //全B 
        int Mid=(L+R)/2;
        int lson=judge(L,Mid,3-opt);
        int rson=judge(Mid+1,R,3-opt);
        if(lson==opt||rson==opt) return opt; //至少有一个必胜态 
        if(lson==rson&&lson==3-opt) return 3-opt; //全为必输态 
        return 3; // 平局
    }
    int main()
    {
        int N; scanf("%d",&N); 
        scanf("%s",c+1); scanf("%s",c+N+1);
        for(int i=1;i<=N+N;i++){
            sum1[i]=sum1[i-1]+(c[i]=='A'?1:0);
            sum2[i]=sum2[i-1]+(c[i]=='B'?1:0);
        }
        int ans=judge(1,2*N,1);
        if(ans==1) printf("Alice
    ");
        if(ans==2) printf("Bob
    ");
        if(ans==3) printf("Draw
    ");
        return 0;
    }
  • 相关阅读:
    C# 操作Orcle数据库
    WinDbg排查CPU高的问题
    NetCore微服务实战体系:日志管理
    NetCore微服务实战体系:Grpc+Consul 服务发现
    解惑求助-关于NetCore2.2中间件响应的问题
    EF Join连接查询的坑
    给DataTable添加行的几种方式
    [C#] 折腾海康威视的人体测温 模组
    [WPF 学习] 15.播放百度合成的语音
    [WPF 学习] 14.PlaceHolder的简单实现
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8857599.html
Copyright © 2020-2023  润新知