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的堆中一共还可以进行j次操作(包括合并和减去),然后进行状态转移即可,注意把情况考虑全

代码:

#include <cstdio>
#include <cstring>

const int N = 1005;

int t, n, a, sum, cnt, dp[55][55 * N];

int solve(int cnt, int sum) {
    if (sum == 1) {//注意这里,如果非1堆减到1了,其实它就变成一个1的堆的
	cnt++;
	sum--;
    }
    if (dp[cnt][sum] != -1) return dp[cnt][sum];
    int &ans = dp[cnt][sum];
    ans = 0;
    if (cnt == 0 && sum == 0) return ans;
    if (cnt >= 1 && !solve(cnt - 1, sum)) ans = 1;//拿掉一个1
    if (sum >= 1 && !solve(cnt, sum - 1)) ans = 1;//在非1堆进行一次操作(合并或减1)
    if (cnt >= 2 && sum > 0 && !solve(cnt - 2, sum + 3)) ans = 1;//两个1合并成一堆,原先有堆
    if (cnt >= 2 && sum == 0 && !solve(cnt - 2, sum + 2)) ans = 1;//两个1合并成一堆,原先没堆
    if (cnt >= 1 && sum > 0 && !solve(cnt - 1, sum + 1)) ans = 1;//一个1和非1堆合并
    return ans;
}

int main() {
    int cas = 0;
    memset(dp, -1, sizeof(dp));
    scanf("%d", &t);
    while (t--) {
	cnt = sum = 0;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
	    scanf("%d", &a);
	    if (a == 1) cnt++;
	    else sum += a;
	}
	if (cnt != n)
	    sum += n - cnt - 1;
	printf("Case #%d: %s\n", ++cas, solve(cnt, sum) ? "Alice" : "Bob");
    }
    return 0;
}

UVA 1500 - Alice and Bob(博弈)

时间: 2024-08-11 09:46:39

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

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 - 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 ga

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最远能走多远(不违反约定),如果无法做到不违反约

HDU 4111 Alice and Bob (博弈)

Alice and Bob Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1799    Accepted Submission(s): 650 Problem Description Alice and Bob are very smart guys and they like to play all kinds of games i

Codeforces 346A Alice and Bob 博弈

http://codeforces.com/problemset/problem/346/A 题意:A和B两个人进行游戏,每人轮流操作,每次从集合(集合元素个数n<=100)取出两个数x,y将|x-y|放入集合中 (|x-y|不存在集合中)不能操作则输 最终游戏结束的标志是无法取出两个数字,他们的差值不在序列中.也就是说,最终状态是一个首项等于公差的等差序列.求出这个等差数列的项数-n,就是游戏进行的回合. 所以我们要先求出首项.设首项为d,接下来就是d+d,d+2d-.后面几项都是首项的倍数

ACdream 1112 Alice and Bob (博弈&amp;amp;&amp;amp;素数筛选优化)

题目链接:传送门 游戏规则: 没次能够将一堆分成两堆 x = a*b (a!=1&&b!=1)x为原来堆的个数,a,b为新堆的个数. 也能够将原来的堆的个数变成原来堆的约数y.y!=x.进行最后一次操作的人获胜. 分析: 也是一个去石头的游戏,因此我们仅仅须要将全部情况的sg值异或起来就好了. 我们首先来考虑一堆.设这一堆的个数为x: 那么全部的情况就是 (a1,x/a1), (a2,x/a2),...,(an,x/an);或者(a1),(a2),..,(an). 由于数据量比較大,我们朴

ACdream 1112 Alice and Bob (博弈&amp;&amp;素数筛选优化)

题目链接:传送门 游戏规则: 没次可以将一堆分成两堆 x = a*b (a!=1&&b!=1)x为原来堆的个数,a,b为新堆的个数. 也可以将原来的堆的个数变成原来堆的约数y,y!=x.进行最后一次操作的人获胜. 分析: 也是一个去石头的游戏,因此我们只需要将所有情况的sg值异或起来就好了. 我们首先来考虑一堆.设这一堆的个数为x: 那么所有的情况就是 (a1,x/a1), (a2,x/a2),...,(an,x/an);或者(a1),(a2),..,(an). 由于数据量比较大,我们朴素

博弈问题-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, ...

ZOJ 3529 A Game Between Alice and Bob (分解质因数+Nim博弈)

A Game Between Alice and Bob Time Limit: 5 Seconds      Memory Limit: 262144 KB Alice and Bob play the following game. A series of numbers is written on the blackboard. Alice and Bob take turns choosing one of the numbers, and replace it with one of