标签:限制 include tac std size stream tor bsp turn
汉诺塔问题比较经典,这里修改--下游戏规则:
#include <iostream> #include <stack> using namespace std; enum Act { // 利用绝对值解决逆操作问题 感谢Stan提供的思路 No = 0, LtoM = 1, MtoL = -1, RtoM = 2, MtoR = -2 }; int move(Act& reAct, Act nowAct, stack<int>& src, stack<int>& dst) { if (!src.empty() && (abs(reAct) != abs(nowAct)) && (dst.empty() || src.top() < dst.top())) { dst.push(src.top()); src.pop(); switch (nowAct){ case 1: cout << "Move " << dst.top() << " from L to M" << endl; break; case -1: cout << "Move " << dst.top() << " from M to L" << endl; break; case 2: cout << "Move " << dst.top() << " from R to M" << endl; break; case -2: cout << "Move " << dst.top() << " from M to R" << endl; break; } reAct = nowAct; return 1; } return 0; } int solveHanoi(int l) { // 解决从左往右移动的问题 stack<int> lS, mS, rS; for (int i = l; i > 0; i--) // 修改此处可决定源栈 { lS.push(i); } Act record = No; int step = 0; while (rS.size() != l) // 修改此处可决定目的栈 { step += move(record, LtoM, lS, mS); step += move(record, MtoL, mS, lS); step += move(record, RtoM, rS, mS); step += move(record, MtoR, mS, rS); } return step; } int main() { cout << "Solve 3 Hanoi LtoR:" << endl; cout << "I will move " << solveHanoi(3) << " steps" << endl; return 0; }
标签:限制 include tac std size stream tor bsp turn
原文地址:https://www.cnblogs.com/MuZiShiYe/p/11270104.html