Hdu1997汉诺塔VII
来源:互联网 发布:男士 爽肤水 知乎 编辑:程序博客网 时间:2024/06/06 00:51
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1997
模拟盘子移动的过程:
1.根据经典的汉诺塔问题来讲,最大编号n的盘子一定是从A移到C,其余n-1个盘子从A通过C移到B,
所以编号为n的盘子如果在B塔上那么直接返回false,否则的话进行第二步
2.n号盘子不在B塔上,假设在A塔上,说明其余n-1个盘子从A通过C移到B,此时,最大编号为n-1;
3.假设在C塔上,说明其余n-1个盘子从B通过A移到C塔,此时最大为n-1
4.重复1,2,3直到n=0,就说明这一个序列可以成功
#include <stdio.h>
int a[3][65];
int hanio(int n,int x,int y,int z)//一开始我是直接传的ABC塔上有多少个盘子,但如果到第二层递归的话,
//再次循环判断的塔永远都会是B塔,所以会出错,后来直接传ABC塔
{
int i;
if(n==0)
return 1;
for(i=1; i<=a[y][0]; i++)
if(a[y][i]==n)
return 0;
for(i=1; i<=a[x][0]; i++)
if(a[x][i]==n)
return hanio(n-1,x,z,y);//因为移动方向的改变,所以要交换BC塔的位置
for(i=1; i<=a[z][0]; i++)
if(a[z][i]==n)
return hanio(n-1,y,x,z);
}
int main()
{
int t,n,m,i,j;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=0; i<3; i++)
{
scanf("%d",&a[i][0]);
for(j=1; j<=a[i][0]; j++)
{
scanf("%d",&a[i][j]);
}
}
if(hanio(n,0,1,2)) printf("true\n");
else printf("false\n");
}
return 0;
}
- HDU1997:汉诺塔VII
- hdu1997汉诺塔VII
- hdu1997汉诺塔VII
- Hdu1997汉诺塔VII
- hdu1997 汉诺塔VII(DFS递归调用)
- hdu1997 汉诺塔VII(深度优先搜索)
- 【(伪)递归】HDU1997 - 汉诺塔VII(非递归解法)
- hdu1997 汉诺塔
- HDU1997 汉诺塔7
- HDU1997
- 汉诺塔VII
- 汉诺塔VII
- 汉诺塔VII
- 汉诺塔VII
- 汉诺塔VII
- 汉诺塔VII
- HDU 1997 - 汉诺塔VII
- HDU 1997 汉诺塔VII
- 数据库元数据的学习
- Main函数中的参数argc,argv的使用详解
- 枚举与switch字符串
- 【C++】多态及其对象模型
- jsp与servlet联合处理
- Hdu1997汉诺塔VII
- Oracle 数据库 练习题6
- BZOJ 2527 Meteors (整体二分)
- ZCMU-1018: 突击战
- gdb调试乱序,value optimized out解决方法
- 推送微信模板消息
- 数组翻转
- unreal engine 4学习笔记:蓝图脚本
- 浅谈HTTP中Get与Post的区别(转)