C :: Aufgabe #55 :: Lösung #1
1 Lösung
#55
Türme von Hanoi - Lösung
Fortgeschrittener - C
von bibir
- 05.09.2014 um 11:28 Uhr
Programmiere die Lösung der Türme von Hanoi.
Dabei ist es Ausreichend, die zu tätigenden Schritte textuell auszugeben
Dabei ist es Ausreichend, die zu tätigenden Schritte textuell auszugeben
#1
von devnull (8870 Punkte)
- 22.11.2014 um 17:52 Uhr
/* hanoi.c : Türme von Hanoi */
#include <stdlib.h>
#include <stdio.h>
char *pnames[3] = {"Links", "Mitte", "Rechts" };
char *position(int p) {
return pnames[p];
}
void move_tower(int ndisk, int from, int to) {
static int count = 0;
int stock;
from %= 3;
to %= 3;
stock = 3 - from - to;
if (ndisk > 0) {
move_tower(ndisk-1, from, stock);
printf("(%d) Setze Scheibe von %s nach %s.\n", ++count, position(from), position(to));
move_tower(ndisk-1, stock, to);
}
}
int main(int argc, char **argv) {
if (argc > 1) {
printf("Turm auf Position %s soll auf Position %s versetzt werden.\n", position(0), position(2));
move_tower(atoi(argv[1]), 0, 2);
} else
printf("Usage: hanoi <Anzahl Scheiben>\n");
return 0;
}
Kommentare:
Für diese Lösung gibt es noch keinen Kommentar
Seite 1 von 0
1
