Created
April 23, 2020 15:03
-
-
Save z-rui/61c004e66b795cffc2cba62325fb3e2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
static void | |
hanoi(unsigned char n) | |
{ | |
void *s[32]; | |
char p[3] = "ABC", t; | |
if (n >= 32) n = 31; | |
s[n] = &&step_3; | |
step_0: | |
if (n == 0) goto *s[n++]; | |
// ABC => ACB | |
t = p[1], p[1] = p[2], p[2] = t; | |
s[--n] = &&step_1; | |
goto step_0; | |
step_1: | |
// A -> C | |
printf("%c -> %c\n", p[0], p[1]); | |
// ACB => BAC | |
t = p[0], p[0] = p[2], p[2] = p[1], p[1] = t; | |
s[--n] = &&step_2; | |
goto step_0; | |
step_2: | |
// BAC => ABC | |
t = p[1], p[1] = p[0], p[0] = t; | |
goto *s[n++]; | |
step_3: | |
return; | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if (argc != 2) | |
fprintf(stderr, | |
"usage: %s [n]\n\tPrint hanoi sequence of n discs.\n", | |
argv[0]); | |
else | |
hanoi(atoi(argv[1])); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment