UVA - 12293 Box Game (规律)

Description

 Box Game 

There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows:

Alice and Bob moves alternatively, Alice moves first. For each move, the player finds out the box having fewer number of balls inside, and empties that box (the balls inside will be removed forever), and redistribute the balls in the other box. After the
redistribution, each box should contain at least one ball. If a player cannot perform a valid move, he loses. A typical game is shown below:

When both boxes contain only one ball, Bob cannot do anything more, so Alice wins.

Question: if Alice and Bob are both clever enough, who will win? Suppose both of them are very smart and always follows a perfect strategy.

Input

There will be at most 300 test cases. Each test case contains an integer
n ( 2n109)
in a single line. The input terminates by n = 0.

Output

For each test case, print a single line, the name of the winner.

Sample Input

2
3
4
0

Sample Output

Alice
Bob
Alice
题意:有两个相同的盒子,其中一个盒子有n个球,另一个有1个球,每次把较小的那个数拿走,然后把较大的数分成两部分,使得操作后每个盒子至少有1个球,如果无法操作,
则当前游戏者输,判断先手(Alice)还是后手(Bob)胜
思路:可以通过SG定理打表找规律,也可以分析,假设当前为3的话,那么这个时候是先手输,然后有可能到达这个状态的有4,5,6,这些是先手胜的情况,那么7的话就是输啦,
依次类推下去,发现当n=2^k-1的时候,是Bob胜,其他的是Alice胜。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

int sg[100], vis[100];

void init() {
	sg[1] = 0;
	sg[2] = 1;
	for (int i = 3; i < 100; i++) {
		memset(vis, 0, sizeof(vis));
		for (int j = (i+1)/2; j < i; j++)
			vis[sg[j]] = 1;
		for (int j = 0; ; j++)
			if (!vis[j]) {
				sg[i] = j;
				break;
			}
		printf("%d %d\n", i, sg[i]);
	}
}

int main() {
//	init();
	int n;
	while (scanf("%d", &n) != EOF && n) {
		if ((n & (n+1)) == 0)
			printf("Bob\n");
		else printf("Alice\n");
	}
	return 0;
}

时间: 2024-10-14 15:53:15

UVA - 12293 Box Game (规律)的相关文章

UVA 12293 - Box Game(博弈)

UVA 12293 - Box Game 题目链接 题意:两个盒子,一开始一个盒子有n个球,一个只有1个球,每次把球少的盒子中球消掉,把多的拿一些球给这个盒子,最后不能操作的输(球不能少于1个),Alice先手,问谁赢 思路:博弈,题目其实可以转化为,给定一个n,每次把减少1到n/2的数字,最后谁是1谁就输了,那么可以去递推前几项找个规律,或者推理,都可以发现只要是2^i - 1的数字Bob就赢,否则Alice赢 代码: #include <stdio.h> #include <stri

uva 12293 - Box Game(组合游戏)

题目链接:uva 12293 - Box Game 题目大意:有两个盒子,第一个盒子装有n个球,第二个盒子装又1个球,每次操作将少的盒子中的球全部拿掉,并从另一个盒子中取一些球放入该盒子,不能使另一个盒子中球的个数为0.两人轮流操作,问说最后谁胜. 解题思路:n如果为2i?1那么先手必败. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; bool judge (

UVA 12293 Box Game(博弈入门)

题目链接:Box Game 题面:            12293 Box Game There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows: Alice and Bob moves al

UVa 1587 Box 盒子

这道题的链接:https://vjudge.net/problem/UVALive-3214 给定6个矩形的长和宽wi与hi(1<=wi, hi<=1000), 判断它们能否构成长方体的6个面 Sample Input 1345 2584 2584 683 2584 1345 683 1345 683 1345 2584 683 1234 4567 1234 4567 4567 4321 4322 4567 4321 1234 4321 1234 Sample Output POSSIBLE

UVa 1587 Box

大水题一发  弄清长方体的几个面的关系就行了 #include<cstdio> #include<algorithm> using namespace std; const int N = 6; struct rec{ int l, w;} r[N]; bool cmp(rec a, rec b) { return a.w < b.w || (a.w == b.w && a.l < b.l); } int main() { int a, b, ok; w

uva 1587 Box(思路)

给6个矩形的长和宽(或者宽和长),问这六个矩形能否组成一个长方体. 思路比较简单,不过需要注意的地方有点多. 首先由于长和宽的顺序为止,所以要处理一下(一开始只处理了后来读入的五组,没有处理单独读入的第一组,差评) 然后要判断能否分成两两相同的三组. 如果能,枚举8种可能的相等的情况. 1 /************************************************************************* 2 > File Name: code/uva/1587.

uva 591 Box of Bricks

Little Bob likes playing with his box of bricks. He puts the bricks one upon another and builds stacks of different height. Look, I've built a wall!'', he tells his older sister Alice.Nah, you should make all stacks the same height. Then you would ha

UVA - 679 Dropping Balls 规律

题目大意:给出一棵完全二叉树,每个节点上都有一个开关,刚开始开关都是开的,球经过该节点后开关就关上了(根节点除外).如果球经过该节点的兄弟节点,那么该节点的开关就又开了 现在往根节点放球,优先选择左边的节点走,如果左边的节点的开关关了,就选择右边的.球落到叶节点后就继续另一颗球,问第n颗球落到了哪个叶节点上 解题思路:其实这题蛮水的,因为是二叉树,所以可以由球的奇偶行来判断球往哪个分支走,如果n % 2 == 0就表示第n颗往右边走了,反之就是往左边走了,以此类推即可推出最后一颗球落到哪了 #i

Uva 1587 - Box ( 思维 )

题意: 给定6个矩形的长和宽wi和hi(1<=wi,hi<=1000),判断它们能否构成长方形的6个面. 脑洞打开~~ /* 1 1 1 1 2 2 2 2 3 3 3 3 */ 1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 #include<cstdlib> 6 #include<cmath> 7 #