UVA - 1500 Alice and Bob (dp+博弈)

Description

Alice and Bob are very smart guys and they like to play all kinds of games in their spare time. the most amazing thing is that they always find the best strategy, and that‘s why they feel bored again and again. They just invented a new game, as they usually
did.

The rule of the new game is quite simple. At the beginning of the game, they write downN random positive integers, then they take turns (Alice first) to either:

  1. Decrease a number by one.
  2. Erase any two numbers and write down their sum.

Whenever a number is decreased to 0, it will be erased automatically. the game ends when all numbers are finally erased, and the one who cannot play in his(her) turn loses the game.

Here‘s the problem: Who will win the game if both use the best strategy? Find it out quickly, before they get bored of the game again!

Input

The first line contains an integer T(1T4000),
indicating the number of test cases.

Each test case contains several lines.

The first line contains an integer N(1N50).

The next line contains N positive integers
A1...AN(1Ai1000),
represents the numbers they write down at the beginning of the game.

Output

For each test case in the input, print one line: `Case #X:Y‘, where
X is the test case number (starting with 1) andY is either "Alice" or "Bob".

Sample Input

3
3
1 1 2
2
3 4
3
2 3 5

Sample Output

Case #1: Alice
Case #2: Bob
Case #3: Bob
题意:有n个数,你可以把一个数减一,或者删掉两个数,然后记上它们的和,两个人轮流操作,不能操作的算输,Alice先手,求结果
思路:如果每堆石子数都大于1,那么最后结果肯定相当于所有的堆合并成一堆后,然后再一个一个拿掉的结果。 
因为如果那种情况是赢的人一定会不断合并堆来确保他是赢的。又因为所有堆的石子数都大于1,所以输的人无法阻止他这么干。 
而有些堆石子数等于1的话,就不一定是所有的合并的结果了,因为输的人可以直接把等于1的堆去掉,就破坏了结构 
(合并相当于2步,去掉只需要1步)。 
dp[i][j]表示有i个石子数为1的堆数,其它堆合并再取完的步数为j。若值为1则先取者胜,为0为先取者输。
那么就有这几种情况:将某堆只能1的拿走;两堆是1的合并;把不是1的减一;把某堆是1的并到不是1的堆上
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;

int dp[60][60000];

int dfs(int i, int j) {
	if (dp[i][j] != -1)
		return dp[i][j];
	if (j == 1)
		return dp[i][j] = dfs(i+1, 0);
	dp[i][j] = 0;
	if (i >= 1 && !dfs(i-1, j))
		dp[i][j] = 1;
	else if (j >= 1 && !dfs(i, j-1))
		dp[i][j] = 1;
	else if (i >= 1 && j > 0 && !dfs(i-1, j+1))
		dp[i][j] = 1;
	else if (i >= 2 && ((j >= 1 && !dfs(i-2, j+3)) || (j == 0 && !dfs(i-2, 2))))
		dp[i][j] = 1;
	return dp[i][j];
}

int main() {
	int t, n, tmp, cas = 1;
	scanf("%d", &t);
	memset(dp, -1, sizeof(dp));
	dp[0][0] = 0;
	while (t--) {
		int a = 0, b = 0;
		scanf("%d", &n);
		for (int i = 0; i < n; i++) {
			scanf("%d", &tmp);
			if (tmp == 1)
				a++;
			else b += tmp + 1;
		}
		if (b)
			b--;
		printf("Case #%d: ", cas++);
		if (dfs(a, b))
			printf("Alice\n");
		else printf("Bob\n");
	}
	return 0;
}

时间: 2024-07-28 14:48:37

UVA - 1500 Alice and Bob (dp+博弈)的相关文章

UVA 1500 - Alice and Bob(博弈)

UVA 1500 - Alice and Bob 题目链接 题意:alice和bob这对狗男女play a game,黑板上有n个数字,每次能把一个数字减1,或者把两个数字合成一个数字,值为两数的和,数字减到0就自动被擦去,最后不能操作的算输,alice先手,问最后谁赢 思路:博弈问题,首先想到一点就很好办了,就是对于非1的所有数,肯定会优先去合并成一个数字的,因为如果当前状态能胜,我优先合并掉,对手不管做什么都无法阻止.然后利用必胜态必败态去进行dp,dp[i][j]记录是有i个1,在非1的堆

uva 1500 - Alice and Bob(推理)

题目连接:uva 1500 - Alice and Bob 题目大意:在黑板上又一个序列,每次操作可以选择一个数减1,或者是合并两个数,一个数被减至1则自动消除,不能操作者输. 解题思路:结论,对于大于1的数可以看成是一个整数s,为消除他们的总操作步数,包括减1以及合并,c为列中1的个数,如果s>2的话,c或者是s为奇数则为必胜,否则必败.若s≤2的话(s=2或者s=0)是,判断c是否为3的倍数,是的话必败,不是的话必胜. 证明:s>2时,s和c均为偶数是为必败态. s为奇数,c为偶数:先手操

uva 1484 - Alice and Bob&#39;s Trip(树形dp)

题目链接:uva 1484 - Alice and Bob's Trip 题目大意:Alice和Bob小两口一起出去旅行,他们从0城市出发,Bob喜欢走比较远的路,因为他是个勤奋的好孩子,Alice喜欢走比较近的路,因为她是一个不勤奋的坏孩子,所以有了意见上的分歧,于是乎在出门前他们约法三章,要求说最后的距离值在[l,r]之间,并且由夫妻两轮流做决定,决定说下一个城市去哪里.现在给出n个城市,以及n-1条边,问说在不让Bob媳妇生气的情况下,Bob最远能走多远(不违反约定),如果无法做到不违反约

XTU 1209 Alice and Bob (博弈)

Alice and Bob Accepted : 174   Submit : 342 Time Limit : 1000 MS   Memory Limit : 65536 KB Problem Description The famous "Alice and Bob" are playing a game again. So now comes the new problem which need a person smart as you to decide the winne

ZOJ 3666 Alice and Bob (SG博弈)

题目: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3666 题意: 给一个有向图,然后A和B轮流移动棋子,棋子在每一个位置可以重叠,当某人不能走时,输! 问A和B谁赢 方法: 显然每一局游戏都是独立的,对每一局游戏异或即可 每一局游戏的结果可以用SG求,记忆化搜索之 1 int dfs(int x) 2 { 3 if (sg[x] != -1) return sg[x]; 4 bool vis[maxn]; 5 me

2011 ACM-ICPC 成都赛区A题 Alice and Bob (博弈动规)

题目大意: 有K堆石子,每堆有Ki个.两人的操作能够是: 1 从某一堆拿走一个 假设该堆在此之后没有石子了.就消失 2 合并两个堆 求是否先手必胜,先手胜输出Alice.否则输出Bob 思路: 这道题读完后毫无头绪.推了半天也推不个所以然来,參看大神代码后,感觉就是一个记忆化搜索啊,唉,知识学多了不会用还是白搭.还得多做题啊! 这里我们把数字分成 1,2,大于等于3的奇数,大于等于4的偶数四类. 这样分的原因在于.一个大于3的奇数是实际上等价于3的:由于每当对手减一个.自己也减一个.就又变回了一

HDU 5708 Alice and Bob (博弈,找规律)

题意: 一个无限大的棋盘,一开始在1,1,有三种移动方式,(x+1,y)(x,y+1) (x+k,y+k)最后走到nm不能走了的人算输.. 析:.我们看成一开始在(n,m),往1,1,走,所以自然可以从1,1,开始递推往出,那么打表程序就出来了.. 打出表以后我们观察到k等于1时稍有特殊,其他则与  (min(cx,cy)&1)^((n+m)&1)) 有关ps(其中cx=n/(k+1),cy=m/(k+1)) 那么就愉快的分类讨论外加试一试和表对照一下就好了.. 代码如下: #includ

UVa 12525 Boxes and Stones (dp 博弈)

Boxes and Stones Paul and Carole like to play a game with S stones and B boxes numbered from 1 to B. Beforebeginning the game they arbitrarily distribute the S stones among the boxes from 1 to B - 1, leavingbox B empty. The game then proceeds by roun

博弈问题-Alice与Bob拿牌游戏

Description Bob and Alice play a game, and Bob will play first. Here is the rule of the game: 1) There are N stones at first; 2) Bob and Alice take turns to remove stones. Each time, they can remove p^k stones. p is prime number, such as 2, 3, 5, ...