https://www.acwing.com/problem/content/131/
每次操作都先操作state2(先出栈),这样会使得字典序较小
#include<iostream>
#include<stack>
#include<vector>
using namespace std;
int n , cnt = 20;
vector<int> state1;//要出栈的顺序
stack<int> state2;//车站
int state3 = 1;
void dfs()
{
if(!cnt) return;//最多输出前20种
if(state1.size() == n)
{
cnt --;
for(auto x : state1) cout << x;
cout << '\n';
return;
}
if(state2.size())
{
state1.push_back(state2.top());
state2.pop();
dfs();
state2.push(state1.back());
state1.pop_back();
}
if(state3 <= n)//state3表示还没进栈的到第几个了
{
state2.push(state3);
state3 ++;
dfs();
state3 --;
state2.pop();
}
}
int main()
{
cin >> n;
dfs();
return 0;
}