boj 1343 汉诺塔 递归问题 谢谢大牛的解答 我需要多联系
2011-03-24 23:15
302 查看
地址:http://acm.scs.bupt.cn/onlinejudge/showproblem.php?problem_id=1343
Tower of HanoiSubmit: 115 Accepted:31Time Limit: 1000MS Memory Limit: 65535K
Description
The Tower of Hanoi is a puzzle consisting of three pegs(namead 1,2,3) and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg(1), the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another peg(3), obeying the following rules:
Only one disk may be moved at a time.
Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg.
No disk may be placed on top of a smaller disk.
For n disks, it is a well-known result that the optimal solution takes 2^n− 1 moves.
Sunny has a YuGong’s spirit, he want to do this day by day by day …... So , he make a tower of hanoi which has n (n<=31) disks , and just wrote down the number of steps he moved before , and this day , he wants to move the ith step to jth step(1<=i<=j<2^n&&j-i<=50) . Now , please tell him each of step he should do .
Input
The first line contains only one integer k , the number of cases.
Each of test case only one line contains three integers n, i , j .
Output
For each test case , first line print “Case d:” , means the dth case.
Next j-i+1 lines print “b->e”, (b,e=1,2,3) , which means move a disk from b peg to e peg .
Sample Input
2
2 1 3
3 1 4
Sample Output
Case 1:
1->2
1->3
2->3
Case 2:
1->3
1->2
3->2
1->3
#include <stdio.h>
// 将N个盘子由Src移动到第Dst根柱子,并输出其中的第i->j步
void Move(int N, int Src, int Dst, int i, int j)
{
int Step = (1 << (N - 1)) - 1; // 计算将N-1个盘子移到中间柱子的所需步数
if (N > 1 && i <= Step) // 如果这个过程中覆盖到了第i->j步,则递归进去,否则没必要
{
Move(N - 1, Src, 6 - Src -Dst, i, j);
}
++Step;
if (i <= Step && Step <= j) // 最底下一个盘子移动到目标
{
printf("%d->%d/n", Src, Dst);
}
// 将N-1个盘子由中间柱子移动到目标柱子
if (N > 1 && j > Step) // 如果这个过程中覆盖到了第i->j步,则递归进去,否则没必要
{
Move(N - 1, 6 - Src - Dst, Dst, i - Step, j - Step);
}
}
int main()
{
int TotalCases;
scanf("%d", &TotalCases);
for (int Case = 1; Case <= TotalCases; ++Case)
{
int n, i, j;
scanf("%d %d %d", &n, &i, &j);
printf("Case %d:/n", Case);
Move(n, 1, 3, i, j);
}
return 0;
}
Tower of HanoiSubmit: 115 Accepted:31Time Limit: 1000MS Memory Limit: 65535K
Description
The Tower of Hanoi is a puzzle consisting of three pegs(namead 1,2,3) and a number of disks of different sizes which can slide onto any peg. The puzzle starts with the disks neatly stacked in order of size on one peg(1), the smallest at the top, thus making a conical shape. The objective of the puzzle is to move the entire stack to another peg(3), obeying the following rules:
Only one disk may be moved at a time.
Each move consists of taking the upper disk from one of the pegs and sliding it onto another peg, on top of the other disks that may already be present on that peg.
No disk may be placed on top of a smaller disk.
For n disks, it is a well-known result that the optimal solution takes 2^n− 1 moves.
Sunny has a YuGong’s spirit, he want to do this day by day by day …... So , he make a tower of hanoi which has n (n<=31) disks , and just wrote down the number of steps he moved before , and this day , he wants to move the ith step to jth step(1<=i<=j<2^n&&j-i<=50) . Now , please tell him each of step he should do .
Input
The first line contains only one integer k , the number of cases.
Each of test case only one line contains three integers n, i , j .
Output
For each test case , first line print “Case d:” , means the dth case.
Next j-i+1 lines print “b->e”, (b,e=1,2,3) , which means move a disk from b peg to e peg .
Sample Input
2
2 1 3
3 1 4
Sample Output
Case 1:
1->2
1->3
2->3
Case 2:
1->3
1->2
3->2
1->3
#include <stdio.h>
// 将N个盘子由Src移动到第Dst根柱子,并输出其中的第i->j步
void Move(int N, int Src, int Dst, int i, int j)
{
int Step = (1 << (N - 1)) - 1; // 计算将N-1个盘子移到中间柱子的所需步数
if (N > 1 && i <= Step) // 如果这个过程中覆盖到了第i->j步,则递归进去,否则没必要
{
Move(N - 1, Src, 6 - Src -Dst, i, j);
}
++Step;
if (i <= Step && Step <= j) // 最底下一个盘子移动到目标
{
printf("%d->%d/n", Src, Dst);
}
// 将N-1个盘子由中间柱子移动到目标柱子
if (N > 1 && j > Step) // 如果这个过程中覆盖到了第i->j步,则递归进去,否则没必要
{
Move(N - 1, 6 - Src - Dst, Dst, i - Step, j - Step);
}
}
int main()
{
int TotalCases;
scanf("%d", &TotalCases);
for (int Case = 1; Case <= TotalCases; ++Case)
{
int n, i, j;
scanf("%d %d %d", &n, &i, &j);
printf("Case %d:/n", Case);
Move(n, 1, 3, i, j);
}
return 0;
}
相关文章推荐
- boj 1343 汉诺塔 递归问题 谢谢大牛的解答 我需要多联系
- boj 1343汉诺塔 递归问题 多谢大牛的代码和讲解 我需要多联系类似题目
- 汉诺塔问题具体解答递归方法
- 递归问题,汉诺塔问题
- 经典递归问题--汉诺塔
- 非递归 汉诺塔问题 C实现方法
- 汉诺塔问题的递归实现
- 递归实现汉诺塔问题
- 递归:解决汉诺塔问题(数据结构3.2 P103)
- Python初学笔记:递归解决汉诺塔问题
- 【经典问题】汉诺塔(递归、C语言实现)
- 从汉诺塔问题谈递归转非递归的通法
- 汉诺塔问题的递归实现(扩展)
- 最近正准备找工作呢,熟悉下递归算法,做了几个递归的例子包括汉诺塔问题
- 汉诺塔问题递归算法分析:
- 用递归解决汉诺塔问题
- 汉诺塔问题以及递归
- 用C语言解决(hanoi)汉诺塔问题——函数的递归调用
- php实现汉诺塔问题(递归)
- [one_demo_10]递归解决汉诺塔问题