1. 程式人生 > >用棧來遞歸 模板 honoi

用棧來遞歸 模板 honoi

char post log include sys nbsp stk esp AC

用棧來模擬遞歸的技巧

#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"); }

用棧來遞歸 模板 honoi