zoukankan      html  css  js  c++  java
  • 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$ \le$N$ \le$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 12 or 3. If the i-th ( 1$ \le$i$ \le$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 

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

    Sample Output 

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

    3个柱子的任意起始和结束状态的的汉诺塔

     1 #include <iostream>
     2 
     3 using namespace std;
     4 
     5 const int maxn=70;
     6 int n,start[maxn],finish[maxn];
     7 
     8 long long f(int* p,int i,int fin)
     9 {
    10     if(i==0) return 0;
    11     if(p[i]==fin)  return f(p,i-1,fin);
    12     else return f(p,i-1,6-p[i]-fin)+(1LL<<(i-1));
    13 }
    14 
    15 int main()
    16 {
    17     int kase=0;
    18     while(cin>>n&&n)
    19     {
    20         for(int i=1;i<=n;i++)
    21             cin>>start[i];
    22         for(int i=1;i<=n;i++)
    23             cin>>finish[i];
    24 
    25         int k=n;
    26         while(k>=1&&finish[k]==start[k])  k--;
    27 
    28         long long ans=0;
    29         if(k>=1)
    30         {
    31             int other=6-finish[k]-start[k];
    32             ans=f(start,k-1,other)+1+f(finish,k-1,other);
    33         }
    34 
    35         cout<<"Case "<<++kase<<": "<<ans<<endl;
    36 
    37     }
    38 
    39     return 0;
    40 }
  • 相关阅读:
    JS各种各样的拖动效果
    GridView实现自动编号
    通用海量数据库翻页
    设置首页和添加到收藏夹的JavaScript代码
    JavaScript弹出窗口总结
    JavaScript获取窗口的高度和宽度
    精妙SQL语句
    Javascript 操作select控件大全(新增、修改、删除、选中、清空、判断存在等)
    GridView数据导入Excel
    网站采集
  • 原文地址:https://www.cnblogs.com/CKboss/p/3084051.html
Copyright © 2011-2022 走看看