zoukankan      html  css  js  c++  java
  • 用栈来递归 模板 honoi

    用栈来模拟递归的技巧

    #define _CRT_SECURE_NO_WARNINGS
    #include<iostream>
    #include<vector>
    #include<cstring>
    #include<set>
    #include<algorithm>
    #include<stack>
    #include<string>
    #include<cstdio>
    #define _for(i, a, b) for (int i = (a); i<(b); ++i)
    using namespace std;
    struct problem {
        int n;
        char scr, mid, dest;
        problem(int nn, char s, char m, char d) :n(nn), scr(s), mid(m), dest(d) {}
    };
    stack<problem> stk;
    
    int main()                                  
    {
        int n;
        cin >> n;
        stk.push(problem(n, 'A', 'B', 'C'));
            while (!stk.empty()) {
                problem now = stk.top();
                stk.pop();
                if (now.n == 1) { cout << now.scr << "->" << now.dest << endl; }
                else {
                    stk.push(problem(now.n - 1, now.mid, now.scr, now.dest));//先放最后一个子问题
                    stk.push(problem(1, now.scr, now.mid, now.dest));
                    stk.push(problem(now.n - 1, now.scr, now.dest, now.mid));
                }
            }
        
        system("pause");
    } 
    成功的路并不拥挤,因为大部分人都在颓(笑)
  • 相关阅读:
    5402.绝对差不超过限制的最长数组
    快乐数
    无重复字符的最长子串
    数组中数字出现的次数
    盛最多的水
    对角线遍历
    LeetCode第24场周赛
    CSS样式
    笔记
    开关电源设计
  • 原文地址:https://www.cnblogs.com/SuuT/p/8707779.html
Copyright © 2011-2022 走看看