星期四, 6月 14, 2007

非遞迴河內塔

呼應上一篇河內塔,偶然間在網路上看到更神的作法。


/* Start post is post 0. */
/* If # posts even, then final post is post 1, else is post 2 */

#include
#include

void main(void)
{
int n, x;

printf( "How many disks? " );
scanf( "%d", &n );
puts( "\n\n" );

for (x=1; x < (1 <<>
printf( "move from pole %i to pole %i.\n",
(x&x-1)%3, ((x|x-1)+1)%3 );
}


The shortest and "mysterious" TH algorithm


An optimal solution to the Towers of Hanoi Puzzle


沒有留言: