Game with a Strip URAL - 2104(神奇的dfs)
2018-01-26 21:34
288 查看
There is a strip 1 × n with two sides. Each square of the strip (their total amount is 2 n, n 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 · 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
input
4
BBAA
BABB
output
Bob
3
AAA
BBB
output
Draw
2
AA
BB
output
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.
题意:折纸片题,纸片正面背面都是AB的组合,Alice和bob折纸片,当纸片正反两面都是该人的幸运色的时候(Alice幸运色为A,bob幸运色为B),则这个人赢,如果折到不能再折的时候正反两面的字母不一样则视为平局。显然每个人都是要向对自己有利的方向去折纸片。使用dfs,如果纸片两面的字母都为偶数的时候才能对折,如果是奇数不能对折,此时需要判断颜色结果。如果A赢返回1,如果B赢返回-1,如果平局返回0,通过比较返回值大小判断哪一面是有利的。当flag 等于0是轮到Alice折,flag等于1是轮到Bob折,使用flag^1异或控制两个人一人折一次。
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
input
4
BBAA
BABB
output
Bob
3
AAA
BBB
output
Draw
2
AA
BB
output
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.
题意:折纸片题,纸片正面背面都是AB的组合,Alice和bob折纸片,当纸片正反两面都是该人的幸运色的时候(Alice幸运色为A,bob幸运色为B),则这个人赢,如果折到不能再折的时候正反两面的字母不一样则视为平局。显然每个人都是要向对自己有利的方向去折纸片。使用dfs,如果纸片两面的字母都为偶数的时候才能对折,如果是奇数不能对折,此时需要判断颜色结果。如果A赢返回1,如果B赢返回-1,如果平局返回0,通过比较返回值大小判断哪一面是有利的。当flag 等于0是轮到Alice折,flag等于1是轮到Bob折,使用flag^1异或控制两个人一人折一次。
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<math.h> int max(int a,int b) { return a > b ? a: b; } int min(int a,int b) { return a <b ? a: b; } char a[1000010],b[1000010]; int dfs(int flag, int n, char*a, char *b) { int l,r; if(n%2 == 1)//不能再折的时候开始判断 { int sum = 0; for(int i=0;i<n;i++) { if(a[i] == 'A') sum++; } for(int i=0;i<n;i++) { if(b[i] == 'A') sum++; } if(sum == 2*n) return 1; else if(sum == 0) return -1; return 0; } l = dfs(flag^1, n/2, a, a+n/2); r = dfs(flag^1, n/2, b, b+n/2); if(flag == 0) return max(l,r); else return min(l,r); } int main() { int n; while(~scanf("%d",&n)) { scanf("%s %s",a,b); int ans = dfs(0,n,a,b); if(ans == 1) printf("Alice\n"); else if(ans == 0) printf("Draw\n"); else printf("Bob\n"); } return 0; }
相关文章推荐
- 【DFS】URAL - 2104 - Game with a Strip
- 【博弈 && dfs】URAL 2104 Game with a Strip
- 【博弈+dfs】Game with a Strip URAL - 2104
- URAL 1382 Game with Cards 解题报告
- 模拟 POJ 2996 Help Me with the Game
- ural Pilot Work Experience(dfs + bfs )
- Poj 2996 Help Me with the Game
- CF-27E - Number With The Given Amount Of Divisors(枚举+dfs)
- POJ2996,Help Me with the Game,棋盘模拟,同水且繁
- ural 1005 dp dfs or 0 1背包
- 快速切题 poj 2996 Help Me with the Game 棋盘 模拟 暴力 难度:0
- Game Programming with DirectX -- 03[谁把我挡住了]
- Confused about GameObject and Transform with Prefab
- 【bzoj2318】Spoj4060 game with probability Problem
- poj 1753 : Flip Game (枚举+dfs)
- URAL 1180 Stone Game 博弈 找规律 大数取模 除3取模
- POJ 2996 Help Me with the Game
- 《game design workshop》阅读笔记(三)——Game Design Basics——Working with Formal Elements
- 《game design workshop》阅读笔记(四)——Game Design Basics——Working with Formal Elements
- URAL 1106 Two Teams (DFS)