/*
汉罗塔问题,要用到一个公式:假设N个盘子都在A上,那么搬到B上则需要2^N - 1次。
dfs(B, pos)的作用是:将前面pos个盘子全部搬到B上所需要多少次。
这样,dfs(B, pos) = dfs(C, pos - 1) + pow(2.0, pos - 1) - 1 + 1;
即,先将pos - 1个盘子都放在C处,然后将最后剩余一个盘子放在B上,再将C上面的盘子都放回B处。
dfs中的B\C具体需要转换
*/
#include <cstdio>
#include <cstring>
#include <cmath>
const int nMax = 65;
char s[nMax];
__int64 dfs(char B, int pos)
{
if(pos == 0)
{
if(s[0] == B) return 0;
else return 1;
}
else
{
if(s[pos] == B) return dfs(B, pos - 1);
else
{
return (__int64)pow(2.0, pos) + dfs(3*'B' - B - s[pos], pos - 1);
}
}
}
int main()
{
while(gets(s))
{
if(s[0] == 'X') break;
int len = strlen(s);
__int64 ans = dfs('B', len - 1);
printf("%I64d\n", ans);
}
return 0;
}
HDU 4260 The End of The World(汉罗塔)
最新推荐文章于 2017-07-18 18:04:00 发布