一.巴什博弈
又一堆石子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;
}