POJ 1704-Georgia and Bob棋子移动(Nim博弈)
2016-05-12 10:12
405 查看
Georgia and Bob
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8996 | Accepted: 2892 |
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?
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
2 3 1 2 3 8 1 5 6 7 9 12 14 17
Sample Output
Bob will win Georgia will win
Source
POJ Monthly--2004.07.18
题目意思:
排成直线的格子上有n个棋子。棋子i在左数第pi个格子中。Georgia和Bob轮流选择一个棋子进行移动。每次可以移动一格及以上任意多格,但是不可以反超其他棋子,也不允许将两个棋子放入同一个格子内。
无法再次进行操作的一方判定为输掉比赛。假设Georgia先进行移动,当双方都采取最优策略时,谁会获胜?
解题思路:
将游戏转换成Nim游戏。棋子从左往右两辆组成一对,每对都看成Nim游戏中的一堆石子,两棋子之间的间隔就是每堆石子的个数。
当棋子个数为奇数的时候,在最右边加一个与最后一个棋子距离为0的棋子。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> using namespace std; #define MAXN 1010 int n,p[MAXN]; void solve() { if(n%2) p[n++]=0;//n为奇数时要补成偶数方便配对 sort(p,p+n); int res=0; for(int i =0;i<n-1;i+=2) res^=(p[i+1]-p[i]-1);//两棋子之间的距离 if(!res) puts("Bob will win"); else puts("Georgia will win"); } int main() { int t; cin>>t; while(t--) { cin>>n; for(int i=0; i<n; ++i) cin>>p[i]; solve(); } return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性