#include <conio.h>
#include <iostream>
std::endl
#include <stack>
using namespace std;
void hanoi(int , char, char, char);
int main()
{ hanoi(3,'A','B','C');
return getch();
}
struct Quad
{ Quad() {}
Quad(int n,char a,char b,char c):_n(n),_x(a),_y(b),_z(c){}
int _n;
char _x, _y, _z;
} Quad;
void hanoi (int n, char x, char y, char z)
{ stack <Quad> s;
s.push(Quad(n,x,y,z));
while (!s.empty())
{ Quad q = s.top();
n=q._n;
x=q._x;
y=q._y;
z=q._z;
if (n == 1)
cout << "pindahkan cakram dari" << q._x
<< "ke" << q._z << "\n";
else
{ s.push(Quad(n-1,y,x,z));
s.push(Quad(1,x,y,z));
s.push(Quad(n-1,x,z,y));
}
}
}
0 comments:
Posting Komentar