# UVa 10795 A different Task: Hanoi & Idea Questions

Source: Internet
Author: User
Tags time limit

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=456&page=show_ problem&problem=1736

The (Three peg) Tower of Hanoi problem is a popular one-in-computer science. Briefly the problem is to transfer all disks from peg-a to peg-c using Peg-b as intermediate one in such A way 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 a ideal example for learning recursion. It is so-studied that one can find the sequence of moves for smaller number of disks such as 3 or 4. A Trivial computer program can find the case of large number of disks also.

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

If more than one disk are in a certain peg, then they'll be in a valid arrangement (larger disk won't be on smaller on ES). We'll give you two such arrangements of disks. You'll have to find the minimum number of moves, which'll transform the one in the arrangement one. Of the course you always have to maintain the constraint, that smaller, disks must upon is larger.

Input

The input file contains at most test cases. Each test case starts with a positive integern (1N60), which means the number of disks. You are given the arrangements in next two lines. Each arrangement would be represented byn integers, which are 1, 2 or 3. If the i-th (1

In) The integer is 1, for should consider that i-th disk are on peg-a. The Input is terminated byn = 0. This case should is processed.

Output

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

Sample Input

```3
1 1 1 2 2 2 3 1 2 3 3 2 1 4 1 1 1 1 1 1 1 1-
0
```

Related Keywords:

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.