Uva----- (10794) a different task

Source: Internet
Author: User

 

A different task

 

The (three PEG) tower of Hanoi Problem is a popular one in Computer Science. briefly the problem is to transfer all the disks from Peg-ATo peg-CUsing PEG-BAs intermediate one in such a way that at no stage a larger disk is above a smaller disk. normally, we want the minimum number of moves required for this task. the problem is used as an ideal example for learning recursion. it is so well studied that one can find the sequence of moves for smaller number of disks such3Or4. A trivial computer program can find the case of large number of disks also.

 


Here we have made your task little bit difficult by making the problem more flexible. Here the disks can be in any peg initially.

 

If more than one disk is in a certain peg, then they will be in a valid arrangement (larger disk will not be on smaller ones ). we will give you two such arrangements of disks. you will have to find out the minimum number of moves, which will transform the first arrangement into the second one. of course you always have to maintain the constraint that smaller disks must be upon the larger ones.

 

Input

The input file contains at most100Test Cases. Each test case starts with a positive integerN(1N60), which means the number of disks. You will be given the arrangements in next two lines. Each arrangement will be representedNIntegers, which are1,2Or3. IfI-Th (1IN) Integer is1, You shoshould consider thatI-Th disk is on Peg-A. Input is terminatedN= 0. This case shocould not be processed.

 

Output

Output of each test case shoshould consist of a line starting'Case #:'Where # Is the test case number. It shoshould be followed by the minimum number of moves as specified in the problem statement.

 

Sample Input

 

31 1 12 2 231 2 33 2 141 1 1 11 1 1 10

 

Sample output

 

Case 1: 7Case 2: 3Case 3: 0

 

Code:

 1 #include<cstdio> 2 const int maxn =70; 3 int n,start[maxn],finish[maxn]; 4 long long Func(int *p,int i,int final) 5 { 6     if(i==0) return 0; 7     if(p[i]==final) return Func(p,i-1,final); 8     return Func(p,i-1,6-p[i]-final)+(1LL<<(i-1)); 9 }10 int main()11 {12     int kase=0;13     while(scanf("%d",&n)==1&&n)14     {15       for(int i=1;i<=n;i++)16             scanf("%d",&start[i]);17       for(int i=1;i<=n;i++)18           scanf("%d",&finish[i]);19       int k=n;20       while(k>=1 && start[k]==finish[k])k--;21       22       long long ans=0;23       if(k>=1)24       {25           int other=6-start[k]-finish[k];26           ans =Func(start,k-1,other)+Func(finish,k-1,other)+1;27       }28       printf("Case %d: %lld\n",++kase,ans);29     }30 }
View code

 

Problem setter: Md. kamruzzaman
Special thanks: Derek kisman (alternate solution), Shahriar Manzoor (Picture Drawing) Miguel Revilla 2004-12-10

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.