ac代码
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<queue>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int i, j, k, n, m, f[31][31];
int main()
{
cin >> n >> m;
memset(f, 0, sizeof(f));
f[1][0] = 1;
for (k = 1; k <= m; k++)
{
f[1][k] = f[2][k - 1] + f[n][k - 1];
f[n][k] = f[n - 1][k - 1] + f[1][k - 1];
for (i = 2; i <= n - 1; i++)f[i][k] = f[i - 1][k - 1] + f[i + 1][k - 1];
}
cout << f[1][m] << endl;
return 0;
}
dfs超时代码
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<queue>
#include<cstring>
#include<string>
#include<map>
using namespace std;
int n, m;
long long ans=0;
void dfs(int id, int c) {
if (c == m && id == 1) {
ans++; return;
}
if (c >= m)return;
if (id == n) {
dfs(1, c + 1);
dfs(n - 1, c + 1);
return;
}
if (id == 1) {
dfs(n, c + 1);
dfs(2, c + 1);
return;
}
dfs(id + 1, c + 1);
dfs(id - 1, c + 1);
}
int main() {
cin >> n >> m;
dfs(1, 0);
cout << ans;
}