UVA 11892 ENimEN (简单博弈)

题面:

11892  ENimEN

In deterministic games no chance is involved, meaning that the final result can be predicted from the

initial arrangement assuming players play optimal. These games are so boring.piloop and poopi are professional gamers. They play games only to study their algorithmic properties.

Their field of expertise is boring games. One of the boring games they often play is Nim. Nim is a

two-player game which is played using distinct heaps, each containing a number of objects (e.g. stones).

Players take turns removing non-zero number of objects from a heap of their choice. The player who

removes the last object will win.

They wonder if they can change the game to make it more fascinating. Would not that be more

interesting if make the rules stricter? For example what if each player is obliged to take objects from

the last non-empty heap as his opponent took objects from. And if there is no such heap, he can choose

one heap freely and take objects from it.ENimEN is their new invented game based on this rule.

If you are interested in ENimEN, write a program to determine the winner given the initial arrange-

ment assuming both players, play optimal. We believe it has also some benefits for you!

Input

The first line contains T(T 100), the number of test cases. Each test begins with an integer

N(N 20000) in the first line, the number of heaps followed by N integersai(1ai10 9), are the number of objects in i-th heap.

Output

If in the optimal strategy the first player is the winner print ‘poopi’ (as he always plays first), otherwise print ‘piloop’. (Quotes for clarity)

Sample Input

2

2

1 1

4

1 2 1 1

Sample Output

piloop

poopi

解题:简单推一下前几组,可以发现只有(1,1),(1,1,1,1),(1,1,1,1,1,1)...时后者才可能赢。也不是简单的找规律,而是在推导过程中根据结果,想原因。这道题是因为先者面对堆1,没有选择,只能直接取掉,而面对非1堆,先者可以选择全部取掉,或者取掉只剩1个,让后者取,故先者可以左右全局,确定全局的步数。只有碰到全1,且1的个数为偶数个时,先者才没有选择的余地。

代码:

#include <iostream>
using namespace std;
int main()
{
	int t,n,tmp,ans;
	cin>>t;
	while(t--)
	{
		cin>>n;
		ans=0;
		bool flag=true;
		for(int i=1;i<=n;i++)
		{
           cin>>tmp;
		   if(tmp!=1)
			   flag=false;
		}
		if(n%2==0&&flag)
			cout<<"piloop\n";
		else cout<<"poopi\n";
	}
	return 0;
}

&#20;

&#20;

&#20;

&#20;

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-06 14:22:20

UVA 11892 ENimEN (简单博弈)的相关文章

uva 11892 - ENimEN(推理)

题目链接:uva 11892 - ENimEN 题目大意:给定n堆石子的个数,两人轮流选择石子堆取石子,直到不能取为失败,附加条件,如果前一次操作,即队手的操作,没有将选中石子堆中的石子取完,那么当前操作者必须在该堆中取石子. 解题思路:只要有一个石子堆的个数大于2,那么先手就获得必胜态,可控.对于全是1的情况判断奇偶性. #include <cstdio> #include <cstring> #include <algorithm> using namespace

UVA - 11892 ENimEN (推理)

Description  ENimEN  In deterministic games no chance is involved, meaning that the final result can be predicted from the initial arrangement assuming players play optimal. These games are so boring. piloop and poopi are professional gamers. They pl

11892 - ENimEN(博弈)

UVA 11892 - ENimEN 题目链接 题意:给定n堆石头,两人轮流取,每次只能取1堆的1到多个,如果上一个人取了一堆没取完,那么下一个人必须继续取这堆,取到最后一个石头的赢,问谁赢 思路:简单推理一下,后手只可能在堆数偶数并且都是1的情况下才可能赢 代码: #include <stdio.h> #include <string.h> const int N = 20005; int t, n, a[N]; bool judge() { if (n % 2) return

UVA 10561 - Treblecross(博弈SG函数)

UVA 10561 - Treblecross 题目链接 题意:给定一个串,上面有'X'和'.',可以在'.'的位置放X,谁先放出3个'X'就赢了,求先手必胜的策略 思路:SG函数,每个串要是上面有一个X,周围的4个位置就是禁区了(放下去必败),所以可以以X分为几个子游戏去求SG函数的异或和进行判断,至于求策略,就是枚举每个位置就可以了 代码: #include <stdio.h> #include <string.h> #include <algorithm> usi

UVA 1558 - Number Game(博弈dp)

UVA 1558 - Number Game 题目链接 题意:20之内的数字,每次可以选一个数字,然后它的倍数,还有其他已选数的倍数组合的数都不能再选,谁先不能选数谁就输了,问赢的方法 思路:利用dp记忆化去求解,要输出方案就枚举第一步即可,状态转移过程中,选中一个数字,相应的变化写成一个函数,然后就是普通的博弈问题了,必胜态之后必有必败态,必败态之后全是必胜态 代码: #include <stdio.h> #include <string.h> const int N = 105

ACM: NBUT 1107 盒子游戏 - 简单博弈

NBUT 1107  盒子游戏 Time Limit:1000MS     Memory Limit:65535KB     64bit IO Format: Practice Appoint description:  System Crawler  (Aug 13, 2016 10:35:29 PM) Description 有两个相同的盒子,其中一个装了n个球,另一个装了一个球.Alice和Bob发明了一个游戏,规则如下:Alice和Bob轮流操作,Alice先操作每次操作时,游戏者先看看

UVa 725 Division --- 简单枚举

题目链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=666 /* UVa 725 Division --- 简单枚举 */ #include <cstdio> #include <cstring> bool used[10]; /* 判断传进来的两个数是否满足条件 */ bool judge(int a, i

HDU 1846 Brave Game (简单博弈)

HDU 1846 Brave Game (简单博弈) ACM 题目地址: HDU 1846 Brave Game 题意: 中文. 分析: 博弈入门. 如果n=m+1,因为最多取m个,所以先拿的人拿多少个,后拿的人能全拿走. 所以判断n%(m+1)即可. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * File: 1846.cpp * Create Date: 2014-09-20 10:05:26 * Descripton: game */ #

UVA 1557 - Calendar Game(博弈dp)

UVA 1557 - Calendar Game 题目链接 题意:给定一个日期,两个人轮流走,每次可以走一月或者一天,问最后谁能走到2001.11.4这个日子 思路:记忆化搜索,对于每个日期,如果下两个状态有一个非必胜态,那么这个状态是必胜态,如果后继状态都是必胜态,那么该状态为必败态 代码: #include <stdio.h> #include <string.h> const int day[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31,