zoukankan      html  css  js  c++  java
  • 【DFS】URAL

    大概就是dfs?当前区间(l,r)的答案可以由(l,m)和(m+1,r)区间推出,如果某个区间已经完全被某种颜色覆盖,那么就返回该颜色。否则按照递归层数判定,奇数层Alice占优势,只需左右区间中一者为必胜即可,而Bob需要左右区间均为其必胜色才可以。无解判一下即可。感觉还是很巧妙。

    There is a strip 1 × n with two sides. Each square of the strip (their total amount is 2 nn squares on each side) is painted in one of two colors (let’s call them Aand 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 ( Arefers 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 · 10 5).
    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”.

    Example

    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.
    #include<cstdio>
    int n,rsz[1000010];
    char a[1000010];
    int dfs(int l,int r,int dep)
    {
    	if(rsz[l]>=r)
    	  {
    	  	if(a[l]=='A')
    		  return 1;
    	  	if(a[l]=='B')
    	  	  return 2;
    	  	return 0;
    	  }
    	if(((r-l+1)/2)%2!=0)
    	  return 0;
    	int m=(l+r>>1);
    	int f1=dfs(l,m,dep^1),f2=dfs(m+1,r,dep^1);
    	if(!dep)
    	  {
    	  	if(f1==1 || f2==1)
    	  	  return 1;
    	  	if(f1==0 || f2==0)
    	  	  return 0;
    //	  	if(f1==2 && f2==2)
    	  	  return 2;
    	  }
    	else
    	  {
    	  	if(f1==2 || f2==2)
    	  	  return 2;
    	  	if(f1==0 || f2==0)
    	  	  return 0;
    //	  	if(f1==1 && f2==1)
    	  	  return 1;
    	  }
    }
    int main()
    {
    	//freopen("e.in","r",stdin);
    	scanf("%d",&n);
    	scanf("%s",a+1);
    	scanf("%s",a+1+n);
    	n<<=1;
    	rsz[n]=n;
    	for(int i=n-1;i>=1;--i)
    	  if(a[i]==a[i+1])
    	    rsz[i]=rsz[i+1];
    	  else
    	    rsz[i]=i;
    	int f=dfs(1,n,0);
    	if(f==0)
    	  puts("Draw");
    	else if(f==1)
    	  puts("Alice");
    	else
    	  puts("Bob");
    	return 0;
    }
  • 相关阅读:
    sqlalchemy 转json 的几种常用方式
    程序员的思维模型指南
    软件的本质
    Python数据模型及Pythonic编程
    Linux Kernel C语言编程范式
    U-Boot内存管理
    Linux网络文件系统的实现与调试
    Linux内核内存管理架构
    Linux多核并行编程关键技术
    Go/Python/Erlang编程语言对比分析及示例
  • 原文地址:https://www.cnblogs.com/autsky-jadek/p/6304320.html
Copyright © 2011-2022 走看看