Update hanoi.cpp
This commit is contained in:
parent
ab120c4000
commit
1d54efe278
16
hanoi.cpp
16
hanoi.cpp
|
|
@ -12,31 +12,26 @@ hanoi::hanoi(int k) {
|
|||
bauen(h);
|
||||
}
|
||||
|
||||
void hanoi::setze(int h, int v, int n, int m)
|
||||
{
|
||||
void hanoi::setze(int h, int v, int n, int m) {
|
||||
if (h != 0) {
|
||||
setze(h - 1, v, m, n);
|
||||
cout << " " << v << " - " << n << endl;
|
||||
this->leg(v, n);
|
||||
this->print();
|
||||
setze(h - 1, m, n, v);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void hanoi::setze_fast(int h, int v, int n, int m)
|
||||
{
|
||||
void hanoi::setze_fast(int h, int v, int n, int m) {
|
||||
if (h != 0) {
|
||||
setze_fast(h - 1, v, m, n);
|
||||
this->leg(v, n);
|
||||
setze_fast(h - 1, m, n, v);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void hanoi::bauen(int h) {
|
||||
for (int k = 0; k <= h; k++)
|
||||
{
|
||||
for (int k = 0; k <= h; k++) {
|
||||
turm1[k] = h-k+1;
|
||||
turm2[k] = 0;
|
||||
turm3[k] = 0;
|
||||
|
|
@ -47,13 +42,10 @@ void hanoi::print() {
|
|||
for (int i = h; i >= 0; i--) {
|
||||
if (i <= h1 && turm1[i]) cout << turm1[i] << "\t";
|
||||
else cout << " " << "\t";
|
||||
|
||||
if (i <= h2 && turm2[i]) cout << turm2[i] << "\t";
|
||||
else cout << " " << "\t";
|
||||
|
||||
if (i <= h3 && turm3[i]) cout << turm3[i] << "\t";
|
||||
else cout << " " << "\t";
|
||||
|
||||
cout << endl;
|
||||
}
|
||||
cout << "##################" << endl;
|
||||
|
|
@ -62,10 +54,8 @@ void hanoi::print() {
|
|||
void hanoi::leg(int v, int n) {
|
||||
if (v == 1 && n == 2 && h1 >= 0) turm2[++h2] = turm1[h1--];
|
||||
if (v == 1 && n == 3 && h1 >= 0) turm3[++h3] = turm1[h1--];
|
||||
|
||||
if (v == 2 && n == 1 && h2 >= 0) turm1[++h1] = turm2[h2--];
|
||||
if (v == 2 && n == 3 && h2 >= 0) turm3[++h3] = turm2[h2--];
|
||||
|
||||
if (v == 3 && n == 1 && h3 >= 0) turm1[++h1] = turm3[h3--];
|
||||
if (v == 3 && n == 2 && h3 >= 0) turm2[++h2] = turm3[h3--];
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue