数据结构作业答案

太弟弟了我,排版不好,直接输出定向到文件得了,栈就直接放在小黑框里,太弟弟了

#include<bits/stdc++.h>
using namespace std;

int cnt = 0;

struct Node {
	int n, from, tmp, to,re;
};

vector<string>Depth, RunID;

stack<Node>stk;

void move(int from, int to) {
	cout << from << "->" << to << "---------------------------------------" <<  endl;
}

int ct = 0;
void show(int n, int from, int tmp, int to, int depth) {
	cout << "递归层次 : " << depth << " : " << endl << endl;
	string x; x += (char)(depth + '0');
	Depth.push_back(x);
	stack<Node>tmpp;

	while (!stk.empty()) {
		Node tp = stk.top(); stk.pop();
		
		cout << "    ";
		cout << tp.re << " " << tp.n << " " << (char)(tp.from + 'a' - 1) << " " << (char)(tp.tmp + 'a' - 1) << " " << (char)(tp.to + 'a' - 1) << endl;
		tmpp.push(tp);
	}
	while (!tmpp.empty()) {
		stk.push(tmpp.top());
		tmpp.pop();
	}
}

void hanoi(int n, int from, int tmp, int to, int depth,int re) {
	//cout << ++cnt << " move " << endl << endl;
	stk.push(Node{ n,from,tmp,to,re });
	if (n != 1) {
		//cout << "1, 2, 4, 5" << endl;
		RunID.push_back("1, 2, 4, 5");
	}
	else {
		//cout << "1, 2, 3, 9" << endl;
		RunID.push_back("1, 2, 3, 9");
	}
	
	if(n != 1)
		show(n, from, tmp, to, depth);
	
	
	cnt++;

	if (n == 1) {
		move(from, to);
		show(n, from, tmp, to, depth);
		Node tp = stk.top();
		stk.pop();
		if (tp.re == 6) {
			//cout << "6, 7" << endl;
			RunID.push_back("6, 7");
		}
		else {
			//cout << "8, 9" << endl;	
			RunID.push_back("8, 9");
		}
		return;
	}
	hanoi(n - 1, from, to, tmp, depth + 1,6);

	move(from, to);

	show(n, from, tmp, to, depth);
	hanoi(n - 1, tmp, from, to, depth + 1,8);
	show(n, from, tmp, to, depth);

	Node tp = stk.top();
	stk.pop();
	if (tp.re == 6) {
		//cout << "6, 7" << endl;
		RunID.push_back("6, 7");
	}
	else {
		//cout << "8, 9" << endl;	
		RunID.push_back("8, 9");
	}
}

int main() {
	int n;
	cout << "输入 n : " << endl;
	cin >> n;
	hanoi(n, 1, 2, 3, 1,0);
	cout << endl << "递归次数 : " << cnt << endl;

	ofstream x;
	x.open("C:\\Users\\SupZ\\Desktop\\t.txt",'w');

	for (int i = 0; i < Depth.size(); i++) {
		x << Depth[i] << "    " << RunID[i] << endl;
	}
}


N = 4的答案,运行层次和 运行语句

层次      语句
1    1, 2, 4, 5
2    1, 2, 4, 5
3    1, 2, 4, 5
4    1, 2, 3, 9
3    6, 7
4    1, 2, 3, 9
3    8, 9
2    6, 7
3    1, 2, 4, 5
4    1, 2, 3, 9
3    6, 7
4    1, 2, 3, 9
3    8, 9
2    8, 9
1    6, 7
2    1, 2, 4, 5
3    1, 2, 4, 5
4    1, 2, 3, 9
3    6, 7
4    1, 2, 3, 9
3    8, 9
2    6, 7
3    1, 2, 4, 5
4    1, 2, 3, 9
3    6, 7
4    1, 2, 3, 9
3    8, 9
2    8, 9
1    8, 9

图照着提示的movei自己画就可以了

递归层次 : 1 :

    0 4 a b c
递归层次 : 2 :

    6 3 a c b
    0 4 a b c
递归层次 : 3 :

    6 2 a b c
    6 3 a c b
    0 4 a b c
1->2---------------------------------------
递归层次 : 4 :

    6 1 a c b
    6 2 a b c
    6 3 a c b
    0 4 a b c
1->3---------------------------------------
递归层次 : 3 :

    6 2 a b c
    6 3 a c b
    0 4 a b c
2->3---------------------------------------
递归层次 : 4 :

    8 1 b a c
    6 2 a b c
    6 3 a c b
    0 4 a b c
递归层次 : 3 :

    6 2 a b c
    6 3 a c b
    0 4 a b c
1->2---------------------------------------
递归层次 : 2 :

    6 3 a c b
    0 4 a b c
递归层次 : 3 :

    8 2 c a b
    6 3 a c b
    0 4 a b c
3->1---------------------------------------
递归层次 : 4 :

    6 1 c b a
    8 2 c a b
    6 3 a c b
    0 4 a b c
3->2---------------------------------------
递归层次 : 3 :

    8 2 c a b
    6 3 a c b
    0 4 a b c
1->2---------------------------------------
递归层次 : 4 :

    8 1 a c b
    8 2 c a b
    6 3 a c b
    0 4 a b c
递归层次 : 3 :

    8 2 c a b
    6 3 a c b
    0 4 a b c
递归层次 : 2 :

    6 3 a c b
    0 4 a b c
1->3---------------------------------------
递归层次 : 1 :

    0 4 a b c
递归层次 : 2 :

    8 3 b a c
    0 4 a b c
递归层次 : 3 :

    6 2 b c a
    8 3 b a c
    0 4 a b c
2->3---------------------------------------
递归层次 : 4 :

    6 1 b a c
    6 2 b c a
    8 3 b a c
    0 4 a b c
2->1---------------------------------------
递归层次 : 3 :

    6 2 b c a
    8 3 b a c
    0 4 a b c
3->1---------------------------------------
递归层次 : 4 :

    8 1 c b a
    6 2 b c a
    8 3 b a c
    0 4 a b c
递归层次 : 3 :

    6 2 b c a
    8 3 b a c
    0 4 a b c
2->3---------------------------------------
递归层次 : 2 :

    8 3 b a c
    0 4 a b c
递归层次 : 3 :

    8 2 a b c
    8 3 b a c
    0 4 a b c
1->2---------------------------------------
递归层次 : 4 :

    6 1 a c b
    8 2 a b c
    8 3 b a c
    0 4 a b c
1->3---------------------------------------
递归层次 : 3 :

    8 2 a b c
    8 3 b a c
    0 4 a b c
2->3---------------------------------------
递归层次 : 4 :

    8 1 b a c
    8 2 a b c
    8 3 b a c
    0 4 a b c
递归层次 : 3 :

    8 2 a b c
    8 3 b a c
    0 4 a b c
递归层次 : 2 :

    8 3 b a c
    0 4 a b c
递归层次 : 1 :

    0 4 a b c
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值