Q.3.4 To solve the problem of hanoi

Q: 

In the classic problem of the Towers of Hanoi, you have 3 rods and N disks of different sizes which can slide onto any tower. The puzzle starts with disks sorted in ascending order of size from top to bottom (e.g., each disk sits on top of an even larger one). You have the following constraints:

  • Only one disk can be moved at a time.
  • A disk is slid off the top of one rod onto the next rod.
  • A disk can only be placed on top of a larger disk.

Write a program to move the disks from the first rod to the last using Stacks

A:

起始状态 :(1~n, 0, 0)

中间状态:(n, 1~n-1, 0)

中间状态:(0, 1~n-1, n)

最后状态:(0, 0, 1~n)

因为要用栈处理,所以将后面的状态先入栈。

#include 
#include  
using namespace std;

typedef struct node {
	int start;
	int end;
	char src;
	char bri;
	char des;
	node() {
	}
	node(int a, int b, char A, char B, char C):start(a), end(b), src(A), bri(B), des(C){
		
	}
}node;

void hanoi(int start, int end, char src, char bri, char des) {
	stack mstack;
	if (end == start) {
		cout<<"Move disk "<


你可能感兴趣的:(Cracking,the,coding,interview,Stack)