POJ 1704 Georgia and Bob (Nim博弈)
来源:互联网 发布:软件系统响应时间 编辑:程序博客网 时间:2024/05/23 00:02
题意:
排成直线的格子上有n 个棋子,棋子i 在左数第pi 个格子上,Georgia 和Bob 轮流选择一个棋子向左移动,每次可以移动一格及以上任意多格,但是不允许反超其他的棋子,也不允许将两个棋子放在同一个格子上。无法进行移动的一方失败,假设Georgia 先进行移动,当双方都采取最优策略时,谁会获胜?
思路:
Nim 博弈:
有n 堆石子,每一堆数量告诉你,一方可以从某一堆拿至少一个石子,谁不能拿了谁输。
这个题可以转换到Nim博弈上。
如果棋子数量是偶数的话,那么棋子前后分别组成一对,中间的空隙数量可以看作一堆石子的数量,因为移动右边的棋子时,相当于拿掉石子,移动左边的棋子虽然会增加棋子,但可以通过移动右边的棋子再次恢复到原状态。
如果棋子数量是奇数的话,那么单独拿出第一个棋子和前面组成一堆, 其余的和偶数一样两两组合就好了。
还是有一些小坑的:
输入可能不是按照递增输入的,需要排序。
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int T, n;int a[1000+7];int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); for (int i = 1; i <= n; ++i){ scanf("%d",a+i); } sort(a+1,a+n+1); int ans = 0; if (n & 1){ ans ^= (a[1]-1); for (int i = 2; i <= n; i += 2){ ans ^= (a[i+1]-a[i]-1); } if (ans == 0) puts("Bob will win"); else puts("Georgia will win"); } else { for (int i = 1; i <= n; i += 2){ ans ^= (a[i+1]-a[i]-1); } if (ans == 0) puts("Bob will win"); else puts("Georgia will win"); } } return 0;}
Georgia and Bob
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 10081 Accepted: 3306
Description
Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, number the grids from left to right by 1, 2, 3, ..., and place N chessmen on different grids, as shown in the following figure for example:
Georgia and Bob move the chessmen in turn. Every time a player will choose a chessman, and move it to the left without going over any other chessmen or across the left edge. The player can freely choose number of steps the chessman moves, with the constraint that the chessman must be moved at least ONE step and one grid can at most contains ONE single chessman. The player who cannot make a move loses the game.
Georgia always plays first since "Lady first". Suppose that Georgia and Bob both do their best in the game, i.e., if one of them knows a way to win the game, he or she will be able to carry it out.
Given the initial positions of the n chessmen, can you predict who will finally win the game?
Georgia and Bob move the chessmen in turn. Every time a player will choose a chessman, and move it to the left without going over any other chessmen or across the left edge. The player can freely choose number of steps the chessman moves, with the constraint that the chessman must be moved at least ONE step and one grid can at most contains ONE single chessman. The player who cannot make a move loses the game.
Georgia always plays first since "Lady first". Suppose that Georgia and Bob both do their best in the game, i.e., if one of them knows a way to win the game, he or she will be able to carry it out.
Given the initial positions of the n chessmen, can you predict who will finally win the game?
Input
The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. Then T cases follow. Each test case contains two lines. The first line consists of one integer N (1 <= N <= 1000), indicating the number of chessmen. The second line contains N different integers P1, P2 ... Pn (1 <= Pi <= 10000), which are the initial positions of the n chessmen.
Output
For each test case, prints a single line, "Georgia will win", if Georgia will win the game; "Bob will win", if Bob will win the game; otherwise 'Not sure'.
Sample Input
231 2 381 5 6 7 9 12 14 17
Sample Output
Bob will winGeorgia will win
Source
POJ Monthly--2004.07.18
[Submit] [Go Back] [Status] [Discuss]
Home Page Go Back To top
0 0
- POJ 1704(Georgia and Bob-Nim博弈)
- poj 1704Georgia and Bob nim博弈
- POJ 1704 Georgia and Bob (Nim博弈)
- POJ 1704 Georgia and Bob (博弈 Nim游戏变形)
- POJ 1704 Georgia and Bob (Nim博弈)
- POJ Georgia and Bob(Nim博弈)
- POJ 1704 Georgia and Bob (博弈:Staircase Nim)
- POJ 1704-Georgia and Bob棋子移动(Nim博弈)
- poj 1704 Georgia and Bob(博弈)
- POJ 1704 Georgia and Bob(博弈)
- POJ 1704 Georgia and Bob(博弈)
- poj1704(Nim博弈)Georgia and Bob
- POJ 1704 Georgia and Bob(nim博弈论)
- POJ 1704 Georgia and Bob <Nim变形>
- POJ 1704 Georgia and Bob (Nim游戏变形)
- POJ 1704 Georgia and Bob(nim变型)
- POJ1704:Georgia and Bob (NIM博弈 类似阶梯博弈)
- poj 1704 Georgia and Bob 博弈
- robotframework使用之 下拉框的选择
- Android 自定义View基础(六)--ViewDragHelper(侧滑栏)
- openGL绘制正弦曲线
- hibernate学习(二)
- 用Python实现大文本文件切割
- POJ 1704 Georgia and Bob (Nim博弈)
- easywechat第三方微信开源框架安装
- 艳鹏谈,小程理--白话比特币原理解密
- 构造方法
- oracle 11g空表不导出
- Java并发编程:volatile关键字解析
- 阿里算法内推笔试题
- iOS开发:修改textfield的placeholder字体和颜色
- 451. Sort Characters By Frequency