Acwing 129. 火车进栈
https://www.acwing.com/problem/content/description/131/
输入样例:
3
输出样例:
123
132
213
231
321
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
const LL MAXN=1e18,MINN=-MAXN,INF=0x3f3f3f3f;
const LL N=200200,M=2020;
LL n,sum=20;
stack<int> st; //入栈
vector<int> v; //出栈
int flag=1; //~n的车辆还未入栈
void dfs()
{
if(!sum) return ;//只要前20种答案
if(v.size()>=n)
{
sum--;
for(auto i:v)
{
cout<<i;
}
cout<<endl;
return ;
}
if(st.size())//入栈数量
{
v.push_back(st.top());//出栈
st.pop();
dfs();
st.push(v.back());//恢复现场
v.pop_back();
}
if(flag<=n)//还有车辆未入栈
{
st.push(flag);//压入栈
flag++;
dfs();
flag--;
st.pop();//恢复现场
}
}
int main()
{
cin.tie(0); cout.tie(0); ios::sync_with_stdio(false);
LL T=1;
//cin>>T;
while(T--)
{
cin>>n;
dfs();
}
return 0;
}