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

 

 

posted @ 2012-08-29 17:53  风之轻吟2012  阅读(318)  评论(0编辑  收藏  举报