博弈论以及sg函数

一.巴什博弈

又一堆石子n个,每次最少取一个,最多取m个。

那么先手的比胜点是n%(m+1)!=0

下面给出例题:(Problem - 767 (nefu.edu.cn)

 ac代码:

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

int main()
{
    int t,n,m,i;
    while(cin>>t)
    {
        for(i=1;i<=t;i++)
        {
            cin>>n>>m;
            if(n%(m+1)==0)
                cout<<"second"<<endl;
            else
                cout<<"first"<<endl;
        }
    }
    return 0;
}

 (Problem - 1070 (nefu.edu.cn)

 ac代码:(巴什博弈的来源)

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int n, a, b;
int dp[N], s[N], sg[N];
int main()
{
	while (cin >> n >> a >> b)
	{
		if (n % (a + b) >= 1 && n %(a + b) <= a) cout << "LOST" << endl;
		else cout << "WIN" << endl;
	}
	return 0;
}

至于NIm博弈和威佐夫博弈因为作业中没有相关题目所以就不给出例题了

对于NIM博弈其比胜点为所有对取异或不为0威佐夫则是奇异局势先手输

奇异局势为已知一个小数,第二个数为1.618*x上取整(一般来说1.618精度就够了,不够就多算几位)

sg函数:

对于每个点的结果是不确定的,我们用sg函数来找该点下一步所有可能的情况,进而判断该点的胜负情况

Problem - 1069 (nefu.edu.cn)

ac代码:

#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
int dp[N], s[N], sg[N];
void getsg()//sg函数板子可以考虑背下来
{
	memset(sg, 0, sizeof(sg));
	for (int i = 1; i <= N; i++) {
		memset(s, 0, sizeof(s));
		for (int j = 2; j <= 24 && dp[j] <= i; j++) {
			s[sg[i - dp[j]]] = 1;
		}
		for (int j = 0; j <= N; j++) {
			if (!s[j]) {
				sg[i] = j;
				break;
			}
		}
	}
}
int main()
{
	dp[0] = 0;
	dp[1] = 1;
	for (int i = 2; i <= 24; i++) {
		dp[i] = dp[i - 1] + dp[i - 2];
	}
	getsg();
	int n;
	while (cin >> n)
	{
		if (sg[n]) cout << "YES" << endl;
		else cout << "NO" << endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值