HDU 4260 The End of The World(汉罗塔)
2012-08-25 19:43
363 查看
/* 汉罗塔问题,要用到一个公式:假设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(汉罗塔)
- HDU 4260(The End of The World-Hanoi塔从中间状态移动)
- HDU 4260 The End of The World
- HDU 4260 The End of The World (递推&递归) #by Plato
- hdu 4260 The End of The World
- HDU 4596 - Yet another end of the world(扩展欧几里得)
- hdu Yet another end of the world(扩展欧几里得定理推论)
- Hdu 4596 Yet another end of the world(数论)
- HDU 4596 Yet another end of the world(一阶不定方程是否可解,gcd)
- HDU 4596 J - Yet another end of the world(数论)
- HDU 4596 Yet another end of the world(解一阶不定方程)
- HDU 4569——Yet another end of the world
- [欧几里得] hdu 4596 Yet another end of the world
- hdu 4596 Yet another end of the world(数论)
- HDU 4596 Yet another end of the world (拓展欧几里得)
- HDU 4596 Yet another end of the world (扩展欧几里德)
- HDU 4596 Yet another end of the world (数学,扩展欧几里德)
- HDU The End of The World【递推】
- HDU-4596 Yet another end of the world (数论)
- hdu 4596 Yet another end of the world 线性同余方程组