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;
}
本文作者:Vivian-0918
本文链接:https://www.cnblogs.com/Vivian-0918/p/18103914
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
2023-03-29 洛谷 P1806 跑步(DP)
2023-03-29 洛谷 P1754 球迷购票问题(DP/Catalan)
2023-03-29 洛谷 P2986 [USACO10MAR] Great Cow Gathering G(树形DP/换根DP)
2023-03-29 洛谷 P1775 石子合并(弱化版)(区间DP)