BZOJ1982 [Spoj 2021]Moving Pebbles 【博弈论】

本文解析了一道关于两人博弈的游戏问题。游戏初始有N堆石子,每轮玩家可以选择一堆移除至少一颗石子,并可将剩余石子自由分配到其他堆中。无法进行移动的玩家则输掉游戏。文章提供了判断哪位玩家会获胜的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目

  1. Moving Pebbles Two players play the following game. At the beginning of the game they start with n (1<=n<=100000) piles of stones. At each step of the game, the player chooses a pile and remove at least one stone from this pile and move zero or more stones from this pile to any other pile that still has stones. A player loses if he has no more possible moves. Given the initial piles, determine who wins: the first player, or the second player, if both play perfectly. 给你N堆Stone,两个人玩游戏. 每次任选一堆,首先拿掉至少一个石头,然后移动任意个石子到任意堆中. 谁不能移动了,谁就输了…

输入格式

Each line of input has integers 0 < n <= 100000, followed by n positive integers denoting the initial piles.

输出格式

For each line of input, output “first player” if first player can force a win, or “second player”, if the second player can force a win.

输入样例

3 2 1 3

输出样例

first player

题解

博弈论的题目总是很神(shao)奇(nao)。。。。
但想清楚总是很简单
①根据博弈的套路,当石子堆两两配对,后手猥琐模仿先手,先手必败
②根据本题特点,石子可以拿取后自由移动;
1、若为奇数,一定不是两两配对,那么先手就可以取掉一点并移动使得堆数-1且两两配对
2、若为偶数,且不两两配对,那么先手可以通过一定操作使得两两配对

综上:只要一开始不是两两配对,先手必胜,否则必败

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u]; k != -1; k = ed[k].nxt)
using namespace std;
const int maxn = 100005,maxm = 100005,INF = 1000000000;
inline int RD(){
    int out = 0,flag = 1; char c = getchar();
    while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
    while (c >= 48 && c <= 57) {out = (out << 1) + (out << 3) + c - '0'; c = getchar();}
    return out * flag;
}
int A[maxn],n;
int main(){
    n = RD();
    if (n & 1) {puts("first player"); return 0;}
    REP(i,n){
        A[i] = RD();
        if (i % 2 == 0 && A[i] != A[i - 1]) {puts("first player"); return 0;}
    }
    puts("second player");
    return 0;
}

转载于:https://www.cnblogs.com/Mychael/p/8282741.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值