博弈论——巴什博弈

版权声明:欢迎转载 https://blog.csdn.net/l18339702017/article/details/82149779

基础巴什博弈:

有一堆石子共有N个。A B两个人轮流拿,A先拿。每次最少拿1颗,最多拿K颗,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N和K,问最后谁能赢得比赛。

例题 :51NOD - 1066 Bash游戏

#include<bits/stdc++.h>
using namespace std;
#define LL long long

const int MAX = 1e6+10;
const int INF = 0x3fffffff;

int main()
{
    int t;
    cin>>t;
    while(t--){
        int n,k;
        scanf("%d%d",&n,&k);
        if(n%(k+1)==0){
            printf("B\n");
        }
        else printf("A\n");
    }
    return 0;
}

进阶巴什博弈

有一堆石子共有N个。A B两个人轮流拿,A先拿。每次只能拿1,3,4颗,拿到最后1颗石子的人获胜。假设A B都非常聪明,拿石子的过程中不会出现失误。给出N,问最后谁能赢得比赛。

例题:51NOD - 1067 Bash游戏 V2

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-----------------"<<endl;

typedef long long ll;
const int maxn = 1e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;


int main(){
	int T ;
	scanf("%d", &T);
	while(T--){
		ll n;
		scanf("%lld", &n);
		ll t = n % 7;
		if(t == 0 || t == 2) cout << "B" << endl;
		else cout << "A" << endl;
	}
	return 0;
}

例题:51NOD - 1068 Bash游戏 V3

遇事不决先打表:

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20

A   A   B   A   A   B   A   A  B    A    A     B     A     A     B     A     A     B    A     A

发现规律:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-----------------"<<endl;

typedef long long ll;
const int maxn = 1e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;


int main(){
	int T;
	scanf("%d", &T);
	while(T--){
		char s[N];
		int sum = 0;
		scanf("%s", s);
		int len = strlen(s);
		for(int i = 0; i < len; i++){
			sum += (s[i] - '0');
		}
		if(sum % 3 == 0) puts("B");
		else puts("A");
	}
	return 0;
}

例题:51NOD - 1070 Bash游戏 V4

表面巴什博弈,斐波那契博弈的板子题

#pragma GCC optimize(2)
#include <bits/stdc++.h>
using namespace std;
#define clr(a) memset(a,0,sizeof(a))
#define line cout<<"-----------------"<<endl;

typedef long long ll;
const int maxn = 1e5+10;
const int MAXN = 1e6+10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9+7;
const int N = 1010;

ll a[N];
void init(){
    a[0] = a[1] = 1;
    for(int i = 2; i <= 60; i++){
        a[i] = a[i-1] + a[i-2];
    }
}

int main(){
	init();
	int T;
	scanf("%d", &T);
	while(T--){
		ll n;
		scanf("%lld", &n);
		int flag = 1;
		for(int i = 1; i <= 60; i++){
			if(a[i] == n){
				flag = 0;break;
			}
		}
		if(flag) puts("A");
		else puts("B");
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/l18339702017/article/details/82149779