In 1883, Edouard Lucas invented, or perhaps reinvented, one of the most popular puzzles of all times – the Tower of Hanoi, as he called it – which is still used today in many computer science textbooks to demonstrate how to write a recursive algorithm or program. First of all, we will make a list of the rules of the puzzle:
• There are three pegs: A, B and C.
• There are n disks. The number n is constant while working the puzzle.
• All disks are different in size.
• The disks are initially stacked on peg A so that they increase in size from the top to the bottom.
• The goal of the puzzle is to transfer the entire tower from the A peg to one of the others pegs.
• One disk at a time can be moved from the top of a stack either to an empty peg or to a peg with a larger disk than itself on the top of its stack.
    A good way to get a feeling for the puzzle is to write a program which will show a copy of the puzzle on the screen and let you simulate moving the disks around. The next step could be to write a program for solving the puzzle in a efficient way. You don’t have to do neither, but only know the actual situation after a given number of moves by using a determinate algorithm.
The Algorithm
    It is well known and rather easy to prove that the minimum number of moves needed to complete the puzzle with n disks is 2n − 1. A simple algorithm which allows us to reach this optimum is as follows: for odd moves, take the smallest disk (number 1) from the peg where it lies to the next one in the circular sequence ABCABC . . . ; for even moves, make the only possible move not involving disk 1.
Input
The input file will consist of a series of lines. Each line will contain two integers n, m: n, lying within
the range [0, 100], will denote the number of disks and m, belonging to [0, 2n − 1], will be the number
of the last move. The file will end at a line formed by two zeros.
Output
The output will consist again of a series of lines, one for each line of the input. Each of them will be formed by three integers indicating the number of disks in the pegs A, B and C respectively, when using the algorithm described above.
Sample Input
3 5
64 2
8 45
0 0
Sample Output
1 1 1
62 1 1
4 2 2

问题链接:UVA254 Towers of Hanoi
问题简述:河内塔问题,给定盘子数量和移动的步数,问盘子的状态。
问题分析:移动步数比较大,用Python语言来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的Python语言程序如下:

# UVA254 Towers of Hanoipeg = []
n, m = 0, 0def hanoi(k, u, v):if (k == 0): returnt = u ^ vif m & 1 << (k - 1):peg[u] -= k - 1peg[t] += k - 1peg[u] -= 1peg[v] += 1hanoi(k - 1, t, v)else:hanoi(k - 1, u, t)while True:n, m = map(int, input().split())if n + m == 0: breakpeg = [0, n, 0, 0]if n & 1: hanoi(n, 1, 2)else: hanoi(n, 1, 3)print(peg[1], peg[2], peg[3])

UVA254 Towers of Hanoi【DFS】相关推荐

  1. Bailian2815 城堡问题【DFS】

    2815:城堡问题 总时间限制: 1000ms 内存限制: 65536kB 描述 1 2 3 4 5 6 7 ############################# 1 # | # | # | | ...

  2. Bailian2816 红与黑【DFS】

    2816:红与黑 总时间限制: 1000ms 内存限制: 65536kB 描述 有一间长方形的房子,地上铺了红色.黑色两种颜色的正方形瓷砖.你站在其中一块黑色的瓷砖上,只能向相邻的黑色瓷砖移动.请写一 ...

  3. NUC1158 Lake Counting【DFS】

    Lake Counting 时间限制: 1000ms 内存限制: 65536KB 通过次数: 1总提交次数: 1 问题描述 Due to recent rains, water has pooled ...

  4. NUC1399 Sum It Up【DFS】

    Sum It Up 时间限制: 1000ms 内存限制: 65535KB 通过次数: 1总提交次数: 1 问题描述 Given a specified total t and a list of n ...

  5. HDU1181 变形课【DFS】(废除)

    新题解参见:HDU1181 变形课[DFS+关系闭包+bitset] 变形课 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 13107 ...

  6. 【DFS】巧妙取量的倒油问题

    题目描述 [题目描述]  有三个容器,容量分别为 a,b,c(a> b > c ),一开始a装满油,现在问是否只靠abc三个容器量出k升油.如果能就输出"yes",并且 ...

  7. [kuangbin]专题三 Dancing Links Squiggly Sudoku HDU - 4069【DFS】【精确覆盖】

    [题目描述] Today we play a squiggly sudoku, The objective is to fill a 9*9 grid with digits so that each ...

  8. 【DFS】不撞南墙不回头—深度优先搜索算法[Deep First Search]

    今天上午听到,那个非常6+1的李咏先生因癌症去世 DFS算法的基本模型 深度下,不撞南墙不回头,就是一直往后找,知道没有路了,向后返回. 想起一首民谣,<可能否>--木小雅 https:/ ...

  9. NUC1333 Knight Moves【DFS】

    Knight Moves 时间限制: 1000ms 内存限制: 65535KB 问题描述 A friend of you is doing research on the Traveling Knig ...

最新文章

  1. ICLR2020放榜 34篇满分论文! 48篇orals,108篇spotlights,531篇poster
  2. CLR探索系列:Windbg+SOS动态调试分析托管代码
  3. Linux常用命令——tar
  4. php输出多维json,json多维数组怎么利用php进行构造
  5. canvas 实现图片局部模糊_Canvas模糊化处理图片、毛玻璃处理图片之stackblur.js
  6. [EDA] 2.2 简单PLD结构原理-潘松版
  7. 单身狗救星!电子科大校长为理工科男脱单提建议
  8. NSArray与NSSet的区别
  9. BZOJ2327: [HNOI2011]勾股定理
  10. VS中的快捷键快速格式化代码,使好看,整齐
  11. kindeditor上传图片php,使用 KindEditor 编辑器PHP语言本地上传图片提示错误
  12. 如何让html 兼容IE和chrome,chrome兼容模式如何切换成IE兼容模式_chrome兼容模式切换成IE兼容模式的方法-系统城...
  13. 固态硬盘SSD格式化后,数据恢复的可能性有多大?
  14. 教你炒股票25:吻,MACD、背弛、中枢
  15. 文件备份云服务器,文件备份云服务器
  16. 读书笔记——《柯维的智慧》格言
  17. 图形的逻辑思维题分类以及思路
  18. php 上传 照片流,在 iPhone 上使用“我的照片流”的方法!
  19. 《娱乐至死》读书笔记(part6)--教育的目的是让学生们摆脱现实的奴役,而现在的年轻人为了适应现实而改变自己
  20. subclipse-1.8.4安装方法

热门文章

  1. 开源WebGIS实施方案
  2. 树莓派+docker+tensorflow
  3. php mysql合同跟踪,使用TCP协议进行路由跟踪
  4. java暂停脚本_用Rhino解析Java中的JavaScript:暂停/恢复脚本
  5. html5移动web开发黑马掌上商城_月入35k大佬总结:web前端必须学习的内容(附全套前端教程)...
  6. ✨Shell脚本实现Base64 加密解密
  7. 小程序向webview传参_微信小程序(1)——web-view和小程序间传递参数、发送消息...
  8. 必须包含数字和字母,字符随意的正则表达式
  9. MySQL为啥不用平衡二叉树_MySQL的索引,为什么是B+而不是平衡二叉树
  10. Keras——用Keras搭建自编码神经网络(AutoEncoder)