#include<bits/stdc++.h>
using namespace std;
//0->in, 1->pop
int ans=0;
void wenhao(int x, int y, stack <int>zhan) {
cout << x << ' ' << y << ' ' << zhan.size() << '\n';
if (y==0) {
ans++;
return ;
}
if (zhan.empty()) {
zhan.push(1);
wenhao(0,y-1,zhan);
zhan.pop();
}
zhan.pop();
wenhao(1,y-1,zhan);
zhan.push(1);
zhan.push(1);
wenhao(0,y-1,zhan);
}
int main() {
int n;
cin >> n;
stack <int>dsb;
wenhao(0,n,dsb);
cout << ans;
return 0;
}
https://www.luogu.com.cn/problem/P1044
好像是爆内存,我的电脑上没有编译错误,交上去是re的,运行到一半的时候会跳出来一提示:
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
我是想模拟加递推解决题目,对于每个状态有pop和push的选择,并保持每个状态栈的长度不变(就是那一坨pop和push(1))
当每个状态栈空了的时侯push(1),当所有元素出栈时(y==0)ans++。
然后就出问题了
3
0 3 0
0 2 1
1 1 0
0 0 1
terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc
--------------------------------
Process exited after 7.036 seconds with return value 3
请按任意键继续. . .