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;
}

 

 

转载于:https://www.cnblogs.com/hzg656343072/archive/2012/08/29/2662483.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
该资源内项目源码是个人的课程设计、毕业设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。 该资源内项目源码是个人的课程设计,代码都测试ok,都是运行成功后才上传资源,答辩评审平均分达到96分,放心下载使用! ## 项目备注 1、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 2、本项目适合计算机相关专业(如计科、人工智能、通信工程、自动化、电子信息等)的在校学生、老师或者企业员工下载学习,也适合小白学习进阶,当然也可作为毕设项目、课程设计、作业、项目初期立项演示等。 3、如果基础还行,也可在此代码基础上进行修改,以实现其他功能,也可用于毕设、课设、作业等。 下载后请首先打开README.md文件(如有),仅供学习参考, 切勿用于商业用途。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值