火车进站(出栈顺序问题)

问题:解决了一个序列进栈出栈的顺序的问题。
思路:
state3是一个序列
state2是栈
state1是出栈后的序列
dfs:每一步都有俩种走法(1.进栈 2.出栈)
mark
代码:

#include <iostream>
#include <algorithm>
#include <stack>
#include <vector>

using namespace std;
vector<int> state1;
stack<int> state2;
int state3=1;
int cnt=20;
int n;
void dfs()
{
    if(!cnt) return ;
    if(state1.size()==n)
    {
        for(auto t:state1) cout<<t;
        cout<<endl;
        cnt--;
        return ;
    }
    
    if(state2.size())
    {
        int t=state2.top();
        state1.push_back(t);
        state2.pop();
        dfs();
        state2.push(t);
        state1.pop_back();
    }
    if(state3<=n)
    {
        state2.push(state3);
        state3++;
        dfs();
        state2.pop();
        state3--;
    }
    
}

int main()
{
    cin>>n;
    dfs();
    return 0; 
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值