Possible duplicate:
How it works? Weird towers of hanoi solution
While surfing Google, I found this interesting solution for Tower Of Hanoi, which does not even use the stack as a data structure.
Can someone explain to me briefly what he is actually doing?
Is this decision acceptable?
the code
#include <stdio.h> #include <stdlib.h> int main() { int n, x; printf("How many disks?\n"); scanf("%d", &n); printf("\n"); for (x=1; x < (1 << n); x++) printf("move from tower %i to tower %i.\n", (x&x-1)%3, ((x|x-1)+1)%3); return 0; }
Update: what does hardcoded number 3 do there?
c iteration towers-of-hanoi
TCM
source share