蓝桥杯练习题——博弈论

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;
}
相关推荐
GG.WinGel3 天前
三元组中心问题
java·数据结构·算法·青少年编程·蓝桥杯
GG.WinGel4 天前
质数数目(蓝桥杯练习题)
java·数据结构·算法·青少年编程·蓝桥杯
Tsuki_tl5 天前
【优选算法 — 双指针】经典例题题解+算法总结
算法·蓝桥杯·力扣·刷题·双指针·1024程序员节·优选算法
十二6 天前
【洛谷题解】P1115 最大子段和(前缀和 + 维护最小前缀)
数据结构·c++·算法·蓝桥杯
青少儿编程课堂9 天前
2026蓝桥杯青少组:校园飞鸟穿越赛解析
蓝桥杯·scratch·少儿编程·图形化编程·蓝桥杯青少组
aqiu11111111 天前
蓝桥杯真题复盘:Fibonacci 数列与黄金分割(C++ 算法详解)
c++·算法·蓝桥杯
aqiu11111111 天前
题目总结:[P8752 [蓝桥杯 2021 省 B2] 特殊年份]
蓝桥杯
青少儿编程课堂12 天前
2026蓝桥杯青少组:校园防疫大作战解析
蓝桥杯·scratch·少儿编程·图形化编程·蓝桥杯青少组
MoRanzhi120313 天前
106.正则问题(分治)
python·算法·正则表达式·蓝桥杯·递归·分治算法·字符串处理
MoRanzhi120314 天前
122.四平方和(暴力枚举)
python·数学·算法·蓝桥杯·完全平方数·暴力枚举·四平方和定理