P1044 [NOIP2003 普及组] 栈
链接:https://www.luogu.com.cn/problem/P1044
两种很好的思路:
代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<math.h>
#include<sstream>
#include<string>
#include<string.h>
#include<iomanip>
#include<stdlib.h>
#include<map>
#include<queue>
#include<limits.h>
#include<climits>
#include<fstream>
#include<stack>
typedef long long ll;
using namespace std;
const int N = 20;
ll dp[N][N];
ll dfs(ll x, ll y)//x:队列里剩下元素的数量;y:栈里剩下的元素的数量
{
if (x == 0)return dp[x][y] = 1;
if (dp[x][y])return dp[x][y];
ll res = 0;
if (y > 0)dp[x][y] += dfs(x, y - 1);
dp[x][y] += dfs(x - 1, y + 1);
return dp[x][y];
}
int main()
{
dp[1][0] = 1;
dp[0][1] = 1;
int n; cin >> n;
cout << dfs(n, 0);
return 0;
}