汉诺塔 X
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 294 Accepted Submission(s): 175
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
__int64 m;
void recursion(int a,int b,int c,__int64 n)
{
__int64 t=pow(double(2),double(n-1));
int i=0;
while(t>m)
{ t=t>>1;i++;}
m=(t^m);
if(m==0)
{
int k=1;
while(t%2==0) k++,t=t>>1;//这个依靠上题的规律
if(i%2)
printf("%d %d %d\n",k,a,b);
else
printf("%d %d %d\n",k,a,c);
return ;
}
if(i%2)//判断与底层的关系
recursion(c,a,b,n-i-1);
else
recursion(b,a,c,n-i-1);
}
int main()
{
__int64 k;
int T;
scanf("%d",&T);
while(T--)
{
scanf("%I64d%I64d",&k,&m);
recursion(1,2,3,k);
}
return 0;
}