分类目录归档:博弈论

洛谷 P1199 三国游戏

https://www.luogu.org/blog/user23248/solution-p1199

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
int n,a[501][501],maxV;
int main(){
    cin>>n;
    for(int i=1;i<=n;i++){
        for(int j=i+1;j<=n;j++){
            cin>>a[i][j];
            a[j][i]=a[i][j];   
        }
    }
    for(int i=1;i<=n;i++){
        sort(a[i]+1,a[i]+n+1);
        maxV=max(maxV,a[i][n-1]);
    }
    cout<<1<<endl<<maxV;
}

洛谷 P1290 欧几里德的游戏

又是一道博弈论:题解:
https://www.luogu.org/blog/DJCreeper/p1290-ti-xie

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<iostream>
#include<algorithm>
using namespace std;
void Stan(int,int);
void Ollie(int,int);
void Stan(int i,int j){
    if(i==j||i/j>=2)cout<<"Stan wins"<<endl;
    else Ollie(max(i-j,j),min(i-j,j));
}
void Ollie(int i,int j){
    if(i==j||i/j>=2)cout<<"Ollie wins"<<endl;
        else Stan(max(i-j,j),min(i-j,j));
}
int main(){
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int v1,v2;
        cin>>v1>>v2;
        if(v1<v2)swap(v1,v2);
        Stan(v1,v2);
    }
}

洛谷 P1288 取数游戏II

博弈论……不会……直接看题解……
https://www.luogu.org/blog/user18431/solution-p1288
https://www.luogu.org/blog/fusu2333/solution-p1288

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#define ll long long
#define pii pair<int,int>
#define PINF 0x7fffffff
#define NINF 0x80000000
using namespace std;
int n, arr[22];
int main() {
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; i++)cin >> arr[i];
    for (int i = 1; i <= n; i++)if (arr[i] == 0) {
        if (i % 2 == 0) {
            cout << "YES";
            return 0;
        }
        break;
    }
    for (int i = n; i >= 1; i--)if (arr[i] == 0) {
        if ((n - i + 1) % 2 == 0) {
            cout << "YES";
            return 0;
        }
        break;
    }
    cout << "NO";
}

洛谷 P1247 取火柴游戏

新技能点:博弈论:内容:Nim游戏
参考链接:https://blog.csdn.net/summer__show_/article/details/70185470
https://baike.baidu.com/item/Nim%E6%B8%B8%E6%88%8F/6737105?fr=aladdin
https://blog.csdn.net/kamisama123/article/details/77649118

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#define ll long long
#define pii pair<int,int>
#define PINF 0x7fffffff
#define NINF 0x80000000
using namespace std;
int k;
int arr[500005];
int main() {
    ios::sync_with_stdio(false);
    cin >> k;
    for (int i = 1; i <= k; i++)cin >> arr[i];
    int re = arr[1];
    for (int i = 2; i <= k; i++)re ^= arr[i];
    if (re == 0) {
        cout << "lose";
        return 0;
    }
    for (int i = 1; i <= k; i++) {
        if ((re^arr[i]) < arr[i]) {
            cout << arr[i] - (re ^ arr[i]) << " " << i << endl;
            arr[i] ^= re;
            for (int i = 1; i <= k; i++)cout << arr[i] << " ";
            break;
        }
    }
}