蓝桥杯练习题——博弈论

1.必胜态后继至少存在一个必败态

2.必败态后继均为必胜态




Nim游戏

思路

2 3,先手必赢,先拿 1,然后变成 2 2,不管后手怎么拿,先手同样操作,后手一定先遇到 0 0

a1 ^ a2 ^ a3 ... ^ an = 0,先手必败,否则先手必胜

cpp 复制代码
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int n;

int main(){
    cin>>n;
    for(int i = 1; i <= n; i++) cin>>a[i];
    int res = a[1];
    for(int i = 2; i <= n; i++) res ^= a[i];
    if(res == 0) cout<<"No";
    else cout<<"Yes";
    return 0;
}

// 求先手必胜第一次怎么拿
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int n;

int main(){
    cin>>n;
    for(int i = 1; i <= n; i++) cin>>a[i];
    int res = a[1];
    for(int i = 2; i <= n; i++) res ^= a[i];
    if(res == 0) cout<<"lose";
    else{
        for(int i = 1; i <= n; i++){
            if((a[i] ^ res) >= a[i]) continue;
            printf("第%d堆 拿%d个\n", i, (a[i] - (a[i] ^ res)));
            a[i] = a[i] ^ res;
            break;
        }
    }
    for(int i = 1; i <= n; i++) printf("%d ", a[i]);
    return 0;
}

台阶-Nim游戏

思路

如果拿偶数的台阶,你拿多少下去,我就拿多少下去,所以只需要看奇数台阶

如果奇数位 a1 ^ a3 ^ a5 ^ ... ^ an = 0,先手必败

cpp 复制代码
#include<iostream>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int n;

int main(){
    scanf("%d", &n);
    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
    int res = a[1];
    for(int i = 3; i <= n; i += 2){
        res ^= a[i];
    }
    if(res) printf("Yes\n");
    else printf("No\n");
    return 0;
}

集合-Nim游戏

思路

如果 SG(x1) ^ SG(x2) ^ SG(x3) ^ ... ^ SG(xn) = 0,先手必败

能到 0 那就是 1,能到 1 那就是 0,同时能到 1 和 0,那就是 2

cpp 复制代码
#include<iostream>
#include<cstring>
#include<unordered_set>
using namespace std;
const int N = 110, M = 1e4 + 10;
int a[N], b[M];
int n, m;

int sg(int x){
    if(b[x] != -1) return b[x];
    unordered_set<int> s;
    for(int i = 0; i < n; i++){
        int t = a[i];
        if(x >= t) s.insert(sg(x - t));
    }
    for(int i = 0; ; i++){
        if(!s.count(i)){
            b[x] = i;
            return b[x];
        }
    }
}

int main(){
    memset(b, -1, sizeof(b));
    scanf("%d", &n);
    for(int i = 0; i < n; i++) scanf("%d", &a[i]);
    scanf("%d", &m);
    int res = 0, x;
    for(int i = 0; i < m; i++){
        scanf("%d", &x);
        res ^= sg(x);
    }
    if(res) printf("Yes\n");
    else printf("No\n");
    return 0;
}

拆分-Nim游戏


思路

一个数分成两个数,再异或

cpp 复制代码
#include<iostream>
#include<cstring>
#include<unordered_set>
using namespace std;
const int N = 110;
int b[N];
int n;

int sg(int x){
    if(b[x] != -1) return b[x];
    unordered_set<int> s;
    for(int i = 0; i < x; i++){
        for(int j = 0; j <= i; j++){
            s.insert(sg(i) ^ sg(j));
        }
    }
    for(int i = 0; ; i++){
        if(!s.count(i)){
            b[x] = i;
            return b[x];
        }
    }
}

int main(){
    memset(b, -1, sizeof(b));
    scanf("%d", &n);
    int res = 0, x;
    for(int i = 0; i < n; i++){
        scanf("%d", &x);
        res ^= sg(x);
    }
    if(res) printf("Yes\n");
    else printf("No\n");
    return 0;
}
相关推荐
依然鸣1 小时前
蓝桥杯多校模拟赛 题解
数据结构·c++·经验分享·学习·算法·蓝桥杯
卡兹墨6 天前
iOS从打包到上架详细流程
ios·职场和发展·蓝桥杯
不正经学生7 天前
二叉树递归遍历:先根与后根,递归思维的最佳试炼场
c语言·开发语言·数据结构·算法·蓝桥杯
Ivanqhz24 天前
NFM(神经因子分解机)
开发语言·javascript·人工智能·算法·蓝桥杯
变量未定义~1 个月前
单调栈、单调队列(模板)、子矩阵(模板)
数据结构·算法·蓝桥杯
智者知已应修善业1 个月前
【P12159蓝桥杯数组翻转】2025-4-24
c语言·c++·经验分享·笔记·算法·蓝桥杯
码云数智-大飞1 个月前
从 OC 平滑迁移 Swift 完整方案
职场和发展·蓝桥杯
嘿黑嘿呦2 个月前
chap 8排序
算法·蓝桥杯·排序算法·软件工程
林森lsjs2 个月前
【日耕一题】5. 青春常数(17届蓝桥杯C++B组第一题)
算法·蓝桥杯
Y_Bk2 个月前
第十七届蓝桥杯C/C++A组省赛
c语言·数据结构·c++·算法·蓝桥杯