(Jizhong) 1597.] [GDKOI2004 Tower of Hanoi (hanoi) [problem] Hanta

(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 N N disk radii equal to each other from 1 1 No. column using 2 2 No. column to all mobile 3 3 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 1 1 No. moved to column 3 3 No. column, does not allow directly from the disk 3 3 No. column to move 1 1 Hao column. The radius of the disk by small to large 1 1 to N N number. With each state N N integers, said first i i integer representation i i numbered column number of the disc is located. then N = 2 N=2 movement scheme is: ( 1 , 1 ) = > ( 2 , 1 ) = > ( 3 , 1 ) = > ( 3 , 2 ) = > ( 2 , 2 ) = > ( 1 , 2 ) = > ( 1 , 3 ) = > ( 2 , 3 ) = > ( 3 , 3 ) (1,1)=>(2,1)=>(3,1)=>(3,2)=>(2,2)=>(1,2)=>(1,3)=>(2,3)=>(3,3) the initial state of 0 0 step, when a programmed number of steps required state.


Input
The first line of the input file integer T ( 1 < = T < = 50000 ) T(1<=T<=50000) , it indicates the number of sets of input data. Next T lines of two integers N , M ( 1 < = n < = 19 , 0 < = M < = N,M(1<=n<=19,0<=M<= Movement of the N number of steps required disc).

Output
Output file has T T line. For each case, the output integer N number of mobile N N disks in M M 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 n = 3 n=3 cases when can be found: the first 1 1 rule of the number that appears is a bit 123321 123321 , the first 2 2 law digit number that appears is 111222333333222111 111222333333222111…… 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");
    }
}
Published 119 original articles · won praise 8 · views 4908

Guess you like

Origin blog.csdn.net/kejin2019/article/details/105013527