(File IO): input: hanoi.in output: hanoi.out
time limit: 1000 ms space constraints: 262144 KB specific restrictions
Goto ProblemSet
Title Description
old Tower of Hanoi problem is this: with the least number of steps will
disk radii equal to each other from
No. column using
No. column to all mobile
No. column, to the top chunk in the market is always in the process of moving. Now coupled with one condition: do not allow directly to disk from
No. moved to column
No. column, does not allow directly from the disk
No. column to move
Hao column. The radius of the disk by small to large
to
number. With each state
integers, said first
integer representation
numbered column number of the disc is located. then
movement scheme is:
the initial state of
step, when a programmed number of steps required state.
Input
The first line of the input file integer
, it indicates the number of sets of input data. Next T lines of two integers
Movement of the N number of steps required disc).
Output
Output file has
line. For each case, the output integer N number of mobile
disks in
state step, between every two numbers separated by a space, at the end of the line and the line do not have extra spaces.
Sample input
. 4
2 0
2. 5
. 3 0
. 3. 1
Sample output
. 1. 1
. 1 2
. 1. 1. 1
2. 1. 1
Data range limit
Problem-solving ideas
: include
cases when can be found: the first
rule of the number that appears is a bit
, the first
law digit number that appears is
and so on.
Code
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#include<cmath>
using namespace std;
int a[6]={1,2,3,3,2,1},t,n,m;
int main(){
freopen("hanoi.in","r",stdin);
freopen("hanoi.out","w",stdout);
scanf("%d",&t);
for(int i=1;i<=t;i++)
{
scanf("%d%d",&n,&m);
for(int j=1;j<=n;j++)
{
printf("%d ",a[m%6]);
m/=3;
}
printf("\n");
}
}