Uva 10795 - A Different Task 【模拟】

A Different Task

\epsfbox{p10795a.eps}

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-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. 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 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 difficult by making the problem more flexible. Here the disks can be in any peg initially.

\epsfbox{p10795b.eps}

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 most 100 test cases. Each test case starts with a positive integer N ( 1≤N≤60), which means the number of disks. You will be given the arrangements in next two lines. Each arrangement will be represented by N integers, which are 1, 2 or 3. If the i-th ( 1≤i≤N) integer is 1, you should consider that i-th disk is on Peg-A. Input is terminated by N = 0. This case should not be processed.

Output

Output of each test case should consist of 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

Sample Output

Case 1: 7

Case 2: 3

Case 3: 0

题意:给你一个汉诺塔的初始态和一个目标态,求最少几步能够从初始态到目标态(移动的过程中还是要保持小的在上面等汉诺塔规则);

思路:对于每一对状态都一个参考态。考虑到最大的盘子如果两个状态都一样那么就不用操作(如果操作就不是最优解),那么我们需要找到两个态不相等的盘子的最大值k(序号)。对于k号盘子,我们需要从(比如)1柱子移动到(比如)2柱子上去,那么k-1号盘子只能放在3号柱子上,那么在3号柱子上就是从小到大依次排放(1, 2…K-1)。那么我们只需要找出来从初始态和目标态到此状态的最少步数之和,然后加1(移动k)。(参考入门经典)

代码:

#include <cstdio>
const int maxn = 70;

long long f(int* p, int k, int final){//final就是p[k]要移动到的柱子的号
    if(k == 0) return 0;
    else if(p[k] == final) return f(p, k-1, final);//如果p[k]在final上,那么就需要移动移动k-1号到final
    else return f(p, k-1, 6-p[k]-final)+(1LL << (k-1));//因为p[k]不等于final, 那么如果要移动前K-1到第三个柱子上中转,然后再将P[K]移动到final柱子上,然后再将k-1个盘子重新移动回来,最后一个步骤是将k-1个盘子从一个柱子上移动到另外一个柱子上,由汉诺塔规律需要移动1<<(k-1)。
}

int st[maxn], en[maxn];

int main(){
    int n, v = 0;
    while(scanf("%d", &n), n){
        for(int i = 1; i <= n; ++ i) scanf("%d", st+i);
        for(int i = 1; i <= n; ++ i) scanf("%d", en+i);
        int k = n;
        while(k>0&&st[k] == en[k]) -- k;
        long long ans  = 0;
        if(k > 0){ //k就是不相等的最大序号
            int final = 6-st[k]-en[k];
            ans += f(st, k-1, final)+f(en, k-1, final)+1;//移动1~k号。
        }
        printf("Case %d: %lld\n", ++v, ans);
    }
    return 0;
} 
时间: 2024-10-28 10:35:56

Uva 10795 - A Different Task 【模拟】的相关文章

uva 10795 A Different Task(递归模拟)

uva 10795 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-A to peg-C using peg-B as intermediate one in such a way that at no stage a larger disk

uva 10795 - A Different Task (递归+状态转移)

题目链接:uva 10795 - A Different Task 思路来源于:点击打开链接 题意: 新汉若塔问题,有n个盘子,放在3个盘子上,给你一个初始状态和一个结束状态,问你最小步数怎样到达. 思路: 递归+状态转移,直接从初态到末态好像不是那么好办,对最大的一块n,首先肯定要把他放在末态的位置上,假设开始在1号位置,要放到3号位置,那么必须先到达这个状态s:1~n-1必须都从大到小放在2上面,然后放n,然后将1~n-1转移到末态,由对称性,也即可以变为末态转移到状态s,那么处理起来就可以

【汉诺塔问题】UVa 10795 - A Different Task

[经典汉诺塔问题] 汉诺(Hanoi)塔问题:古代有一个梵塔,塔内有三个座A.B.C,A座上有64个盘子,盘子大小不等,大的在下,小的在上.有一个和尚想把这64个盘子从A座移到B座,但每次只能允许移动一个盘子,并且在移动过程中,3个座上的盘子始终保持大盘在下,小盘在上.在移动过程中可以利用B座,要求打印移动的步骤.如果只有一个盘子,则不需要利用B座,直接将盘子从A移动到C. 如果有2个盘子,可以先将盘子1上的盘子2移动到B:将盘子1移动到c:将盘子2移动到c.这说明了:可以借助B将2个盘子从A移

UVa 10795 - A Different Task

题目大意:给出n,表示说有n个大小不同的盘子,然后再给出每个盘子的初始位置和目标位置,要求计算出最少的步数使得每个盘子都移动到它的目标位置. 分析:  首先找最大不在目标柱子上的盘子K,因为如果最大的盘子在目标柱子上它不需要移动,也不碍事. 因此问题就成了把K移动到目标柱子,把1到(k-1)移动到中转柱子,所以假设K从A移动到B,A只有K,B是空的,C上面是K-1到1,把这个局面称为参考局面.因为移动是对称的,所以从参考局面移到目标局面与目标局面移到参考局面是一样的步数. 所以问题变成答案=从初

UVA 10795 A Different Task(新汉诺塔问题)

 题目大意:就是普通的汉诺塔问题,给出n,表示说有n个大小不同的碟子,然后再给出每个碟子的初始位置和目标位置,要求计算出最少的步数使得每个碟子都移动到它的目标位置. 思路:考虑编号最大的盘子,如果它在初始位置和目标局面在同一根柱子上,那么我们不需要移动它. 由于盘子的移动是可逆的,根据对称性,我们只需要求出初始局面和目标局面移动形成的参考局面的步数之和,然后加一即可. 我们可以写一个函数f(P, i, final)表示已知各盘子初始编号为P,把1,2,3....i移动到final柱子上所需要

二分图匹配(匈牙利算法) UVA 670 The dog task

题目传送门 1 /* 2 题意:bob按照指定顺序行走,他的狗可以在他到达下一个点之前到一个景点并及时返回,问狗最多能走多少个景点 3 匈牙利算法:按照狗能否顺利到一个景点分为两个集合,套个模板 4 */ 5 #include <cstdio> 6 #include <algorithm> 7 #include <cstring> 8 #include <cmath> 9 #include <vector> 10 using namespace

UVA 10142 Australian Voting(模拟)

题意:澳大利亚投票系统要求选民们将所有候选人按愿意选择的程度排序,一张选票就是一个排序.一开始,每张选票的首选项将被统计.若有候选人得票超过50%,他讲直接胜出:否则,所有并列最低的候选人出局,而那些将出局候选人排在第一位的选票将被重新统计为排名最高的未出局候选人.这一筛选过程将持续进行,直到某个候选人得到超过50%的选票,或所有候选人得票相同. #include<cstdio> #include<cstring> #include<iostream> #include

UVa新汉诺塔问题(A Different Task,Uva 10795)

主要需要理递归函数计算 1 #define MAXN 60+10 2 3 #include<iostream> 4 using namespace std; 5 6 int n,k,S[MAXN],F[MAXN]; 7 8 long long f(int *P,int i,int final) 9 { 10 if(i==0) return 0; 11 if(P[i]==final) return f(P,i-1,final); 12 return f(P,i-1,6-P[i]-final)+(

[ACM] ZOJ 3844 Easy Task (模拟+哈希)

Easy Task Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given n integers. Your task is very easy. You should find the maximum integer a and the minimum integer b among these n integers. And then you should replace both a and bwith a-b. Yo