zoukankan      html  css  js  c++  java
  • hdu 4260 汉诺塔问题 The End of The World

    http://acm.hdu.edu.cn/showproblem.php?pid=4260

    The End of The World

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 259    Accepted Submission(s): 128

    Problem Description
    Legend says that there is a group of monks who are solving a large Towers of Hanoi puzzle. The Towers of Hanoi is a well-known puzzle, consisting of three pegs, with a stack of disks, each a different size. At the start, all of the disks are stacked on one of the pegs, and ordered from largest (on the bottom) to smallest (on the top). The object is to move this stack of disks to another peg, subject to two rules: 1) you can only move one disk at a time, and 2) you cannot move a disk onto a peg if that peg already has a smaller disk on it. The monks believe that when they finish, the world will end. Suppose you know how far they’ve gotten. Assuming that the monks are pursuing the most efficient solution, how much time does the world have left?
     
    Input
    There will be several test cases in the input. Each test case will consist of a string of length 1 to 63, on a single line. This string will contain only (capital) As, Bs and Cs. The length of the string indicates the number of disks, and each character indicates the position of one disk. The first character tells the position of the smallest disk, the second character tells the position of the second smallest disk, and so on, until the last character, which tells the position of the largest disk. The character will be A, B or C, indicating which peg the disk is currently on. You may assume that the monks’ overall goal is to move the disks from peg A to peg B, and that the input represents a legitimate position in the optimal solution. The input will end with a line with a single capital X.
     
    Output
    For each test case, print a single number on its own line indicating the number of moves remaining until the given Towers of Hanoi problem is solved. Output no extra spaces, and do not separate answers with blank lines. All possible inputs yield answers which will fit in a signed 64-bit integer.
     
    Sample Input
    AAA BBB X
     
    Sample Output
    7 0
     
    Source
     
    Recommend
    liuyiding
     
    很纠结啊...看了别人的代码,现在还不是很明白一些细节的东西。
    不懂的还得多琢磨琢磨!
    #include <iostream>
    #include <string>
    using namespace std;
    
    char  str[70];
    int k;
    __int64 sum;
    
    __int64 cnt(int n)         //听说pow()函数数据大了会出错,所以自己写个
    {
        __int64 tmp=1;
       for(int i=0;i<n;i++)
           tmp*=2;
       return tmp;
    }
    
    void fuc(char s,char t,int n)   //主要的递归函数
    {
       if(n==0)
           return ;
       char tmp;
       for(tmp='A';tmp<='C';tmp++)   //找出辅助盘
           if(tmp!=t&&tmp!=s)
               break;
        if(str[n-1]==s)           //判断最下面一个盘的位置
        {
            sum+=cnt(n-1);         //辅助盘到t(目标盘)的步数 2的n-1次方-1 + 最下面的盘子移动一次 为1 =  2的n-1次方
            fuc(s,tmp,n-1);        //s(起点)到辅助盘移动的步数 递归
        }
        else if(str[n-1]==t)
        {
            fuc(tmp,t,n-1);  //省掉s(起点)到辅助盘移动的步骤
        }
        else 
        {
            sum+=cnt(n-1);        
            fuc(tmp,t,n-1);    //最下面的盘子在辅助盘上的情况, 我试了 tmp-->t 和tmp-->s  都能AC...晕了...
                                // 不过还是    fuc(tmp,t,n-1);   容易想一些,因为最终还是要移动到目标盘t上的
        }
    }
    
    int main()
    {
        while(scanf("%s",str)==1&&str[0]!='X')
        {
            sum=0;
            k=strlen(str);
            fuc('A','B',k);       //表示有k个盘子,要从A盘移动到B盘
            printf("%I64d\n",sum);
        }
        return 0;
    }
  • 相关阅读:
    每日Linux命令不完整命令
    mysql安装
    自动调用杀毒软件对文件进行杀毒
    正则获得字符串数组,以字符串分隔获取
    利用SQL语句查找某数据库中所有存储过程包含的内容
    TextBox的滚动条自动到最底部、利用枚举获取HashTable中的值
    Ajax中如何使用Session变量,Cookies可以用表单验证的方式获取并使用。
    用SQL语句批量生成一个表的INSERT语句
    利用DataSet、DataTable、DataView按照自定义条件过滤数据
    向线程传递数据与线程用回调方法检索数据
  • 原文地址:https://www.cnblogs.com/hzg656343072/p/2662483.html
Copyright © 2011-2022 走看看