1th Step: Initial state
2nd step: Move the upper n-1 to the 3rd pole.
3rd Step: Move the nth disk from 1 to 2
4th step: Move the front n-1 from 3 to 1, give way to the first disk.
5th step: Move the nth disk from 2 to 3
6th step: Move the front n-1 from 3 to complete the move
We set F (n) to the number of steps required to move n disks from left (1) to 3, and of course the number of steps to move from 3 to 1.
If you want to move the nth disk from left (1) to 3, you need to move the front n-1 from left (1) to the right (3), then from right (3) to left (1), and then from left (1) to right (3).
The nth disk goes from left (1) to Medium (2) and then right (3) through 2 steps.
So, f (n) =3*f (n-1) +1; F (n) =3^n-1 can be obtained by mathematical calculation;
#include
int main ()
{
int i,n;
__int64 f[36];
f[1]=2;
for (i=2;i<36;i++)
f[i]=3*f[i-1]+2;
while (scanf ("%d", &n)!=eof)
{
printf ("%i64d\n", F[n]);//f (N) =3^n-1;
}
return 0;
}