zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    手动删除木马程序
    病毒注册表常用目标Svchost和Explorer
    对电脑假死现象的修复
    "添加与删除程序"报rundll32错误
    通过注册表regedit对Windows回收站进行恢复
    Win7的话,可能有十种简单的方法进行提速呢
    Windows死机的话,可能的一些猫病
    Android开发发布真机调试
    Java Web-----JSP与Servlet(一)
    Java——Log4j与Log4j2
  • 原文地址:https://www.cnblogs.com/hua-dong/p/8857599.html
Copyright © 2011-2022 走看看