Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
dqx-1005 authored Sep 7, 2023
1 parent b7c73f6 commit 332b493
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions 基础数据结构/栈/非递归汉诺塔.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//https://pintia.cn/problem-sets/15/exam/problems/821?type=7&page=0

//迭代的汉诺塔问题
//用栈模拟递归
#include<bits/stdc++.h>
using namespace std;

//定义汉诺塔的移动结构
struct Move {
int n; //移动盘子数
char start; //起始柱子
char middle; //辅助柱子
char target; //目标柱子
};

stack<Move>moves;
//迭代,用栈模拟递归
int Hanoi(int n,char start,char middle,char target) {

int moveCount = 0;

//初始化将整个问题看作一个移动操作
Move initial_Move = {n,start,middle,target};
moves.push(initial_Move);
while(!moves.empty()) {
Move cur_Move = moves.top();
moves.pop();

n = cur_Move.n;
start = cur_Move.start;
middle = cur_Move.middle;
target = cur_Move.target;

if(n!=1) {
//将问题分解为三个子问题,按照相反的顺序推入堆
Move move1 = {n-1,middle,start,target}; //若中间有盘子,移向目标
Move move2 = {1,start,middle,target}; //将最小盘子移向目标
Move move3 = {n-1,start,target,middle}; //将起始盘子移向辅助柱子
moves.push(move1);
moves.push(move2);
moves.push(move3);
} else {
//cout << "移动盘子 " << " 从 " << start << " 到 " << target << endl;
printf("%c -> %c\n",start,target);
moveCount++;
}
};
return moveCount;
}
int main() {
int n;
//cout<<"输入移动的盘子数:";
cin>>n;
int myCount = Hanoi(n,'a','b','c');
//cout << "总移动次数:" << myCount << endl;
return 0;
}

0 comments on commit 332b493

Please sign in to comment.