码迷,mamicode.com
首页 > 其他好文 > 详细

【DFS】URAL - 2104 - Game with a Strip

时间:2017-01-19 07:49:09      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:nes   put   ext   bing   ase   game   his   start   problem   

大概就是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;
}

【DFS】URAL - 2104 - Game with a Strip

标签:nes   put   ext   bing   ase   game   his   start   problem   

原文地址:http://www.cnblogs.com/autsky-jadek/p/6304320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!