b592:
The Tower of Hanoi

標籤
:

通過比率 : 78% (21 人 / 27 人 ) (非即時)

評分方式：

最近更新 : 2015-10-06 07:57

通過比率 : 78% (21 人 / 27 人 ) (非即時)

評分方式：

Tolerant

最近更新 : 2015-10-06 07:57

內容

The Tower of Hanoi problem is a popular one in computer science. Brieﬂy speaking, the problem is to transfer all the disks from peg-A to peg-C using peg-B as intermediate one in such a way that at no stage a larger disk is above a smaller disk. The minimum number of moves is required for this task.

It is so well 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 have made your task little bit difcult by making the problem more ﬂexible. Here the disks can be placed 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. Your task is 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.

輸入說明

The input data consists of several test cases. Each test case contains three lines.

The first line contains a positive number* n*, 1 <=* n* <= 30, which denotes the number of disks. The two arrangements are denoted by the next two lines. Each arrangement will be represented by* n* integers. The value of integer is 1, 2 or 3, which denote the disk is placed in peg-A, peg-B and peg-C respectively. For example, if the* i*-th (1 <=* i* <=* n*) integer is 1, the* i*-th disk is in peg-A. The figure shown below is an example of test case. It can be described as follows and the minimum number of moves is 6.

4

1 1 1 1

1 2 2 1

Input is terminated by a case where the value of* n* is zero. This case should not be processed.

輸出說明

For each test case, output the minimum number of moves as specified in the problem statement.

範例輸入

4 1 1 1 1 1 2 2 1 3 1 1 1 2 2 2 3 1 2 3 3 2 1 4 1 1 1 1 1 1 1 1 0

範例輸出

6 7 3 0

測資資訊：

記憶體限制：
64
MB

公開 測資點#0 (100%): 10.0s , <1K

公開 測資點#0 (100%): 10.0s , <1K

編號 | 身分 | 題目 | 主題 | 人氣 | 發表日期 |

沒有發現任何「解題報告」 |