巴什博奕。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
bool ways[1002];
int main()
{
#ifdef test
freopen("input.txt", "r", stdin);
#endif
int c, n, m;
scanf("%d", &c);
ways[0] = false;
while(c--)
{
scanf("%d%d", &n, &m);
for(int i=1; i<=n; ++i)
{
ways[i] = false;
for(int j=1; j<=m; ++j)
if(i>=j && !ways[i-j])
{
ways[i] = true;
break;
}
}
if(ways[n])
printf("first\n");
else
printf("second\n");
}
return 0;
}
巴什博奕算法实现

本文介绍了一种解决巴什博奕问题的算法实现。通过动态规划的方式预处理每种状态下先手玩家是否有必胜策略,进而判断双方输赢。代码使用C++编写,适用于竞赛编程及算法学习。
2168

被折叠的 条评论
为什么被折叠?



