#include <iostream>
#include <string>
#include <queue>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <algorithm>
#define maxn 35
using namespace std;
int n, m;
long long dp[maxn][maxn];
int main()
{
//freopen("data.in","r",stdin);
ios::sync_with_stdio(false);
cin >> n >> m;//n个人,传m次
memset(dp, 0, sizeof(dp));
//传递0次,娃娃在1号手上方案数
dp[0][1] = 1;
for(int j = 2; j <= n; ++j) {
dp[0][j] = 0;
}
for(int i = 1; i <= m; ++i) {//传递次数
for(int j = 1; j <= n; ++j) {//在j号人手上
if(j == 1) {
dp[i][j] = dp[i-1][j+1] + dp[i-1][n];
continue;
}
if(j == n) {
dp[i][j] = dp[i-1][1] + dp[i-1][j-1];
continue;
}
dp[i][j] = dp[i-1][j-1] + dp[i-1][j+1];
}
}
cout << dp[m][1] << endl;
return 0;
}
计蒜客 蒜头君的新游戏(DP)
最新推荐文章于 2023-11-11 13:00:32 发布
本文介绍了一个关于游戏的算法问题,通过动态规划求解n个人传递物品m次后,物品回到第一个人手中的方案数。使用C++实现,涉及动态规划等核心概念。
&spm=1001.2101.3001.5002&articleId=80893168&d=1&t=3&u=6a9240337bd244dda67fcfb27416fdcf)
2451

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



