汉诺塔问题【数据结构-朱战立】

【数据结构】汉诺塔问题(丰富图解) 经典汉诺塔问题,丰富图解,助你理解。 阅读详情
/*p137第六章*/

//汉诺塔问题

#include <stdio.h>

/*移动思路*/
/*先将N-1个盘子移动到B(中间柱),再将第N个盘子移动到C柱,再将B柱上的N-1个盘子通过A柱的帮助移动到C柱上。*/
/*典型的递归问题*/
int Hannuota(int number, char frist, char second, char thrid)
{
	if (number<=0)										//边界条件;
	{
		printf("输入错误!\n");
		return 0;
	}
	else if (number==1)                                    //当只有一个盘子的时候直接从A柱到C柱;
	{
		printf("%s:%c->%c\n","移动顺序",frist,thrid);
		return 1;
	}
	Hannuota(number - 1, frist, thrid, second);        //借助C柱将n-1个盘子移动到B柱子;
	printf("%s:%c->%c\n", "移动顺序", frist, thrid);
	Hannuota(number - 1,second ,frist,thrid);         //借助A柱将n-1个盘子移动到C柱子;
}


/*测试主函数*/
int main()
{
	if ((Hannuota(10, 'A', 'B', 'C')) == 0)
	{
		printf("输入错误!");
		getchar();
		return 0;
	}
	getchar();
	return 0;
}

 

【C语言】汉诺塔问题/数据结构经典问题/详细总结讲解 汉诺塔问题是一个经典的递归问题,源于印度的一个古老传说。这个问题的特点是其独特的解决方式,需要将一堆盘子从一个柱子移动到另一个柱子,每次只能移动一个盘子,并且不能将一个较大的盘子放在较小的盘子上面。这个问题的解决方案不仅展示了数学的逻辑美,也为我们提供了一个理解递归和栈结构的好机会。 阅读详情

相关推荐

数据结构——递归学习和汉诺塔问题

递归调用的函数调用过程: 1、保留本层参数和返回地址(将所有的实在参数、返回地址等信息传递给被调函数保存); 2、给下层参数赋值(为被调用函数的局部变量分配存储区); 3、将程序转移到被调函数的入口; 4、保存被调函数的计算结果; 5、恢复上层参数(释放被调函数的数据区); 6、依照被调函数保存的返回地址,将控制转移回调用函数; 汉诺塔问题: 递归问题的优点 对递归...

秋水惊鸿的博客 2535

数据结构-使用c语言 朱战立 pdf,数据结构-使用C语言朱战立.ppt

教材:教材: 朱战立编著,数据结构朱战立编著,数据结构————使用使用 C C语言(第语言(第3 3版),西安交通大版),西安交通大 学出版社,学出版社,20032003年年 数数 据据 结结 构构 2 学时数:70(50学时授课+20学时上机) 教材:朱战立编著,数据结构(使用C语言)第 3版,西 安交通大学出版社 ,2003年 参考书: [1]严蔚敏等,数据结构(C语言版),清华大...

weixin_34435322的博客 1502

适配adv7280m相机到rk3588的安卓平台

Kernel-5.10/arch/arm64/configs/firefly_defconfig里面增加配置CONFIG_VIDEO_ADV7180=y。出现一次开不了机器的情况,接调试口发现,在set_fmt里面用s_ctrl对mipi的频率进行设置会出现异常。设备树修改kernel-5.10/arch/arm64/boot/dts/rockchip/13.dts。把MEDIA_ENT_F_ATV_DECODER改回MEDIA_ENT_F_CAM_SENSOR。第二次编译,仅仅刷新boot.img。

chenyq14的专栏 2449

算法与数据结构面试宝典——经典汉诺塔问题详解

在归并排序和构建二叉树中,我们都是将原问题分解为两个规模为原问题一半的子问题。然而对于汉诺塔问题,我们采用不同的分解策略。!!!例如f3代表将3个圆盘从A移动至C的汉诺塔问题。

白话机器学习 429

在工作台 LS-DYNA 中检索和变换反作用力分量

在结构仿真中,通常需要由施加的位移或速度边界条件产生的反作用力。此外,通常需要根据用户定义的坐标系解析反作用力分量。在本文中,详细介绍了在Workbench (WB) LS-DYNA 中,相对于用户定义的坐标系检索和变换反作用力分量的过程。

jkh920184196的博客 885

学习笔记:物理信息神经网络(PINN)

PINN 的强大在于它打破了传统数值方法的限制,将神经网络的非线性拟合能力与物理方程的严谨性结合起来。它在处理高维、数据稀缺、复杂物理约束的场景下尤其出色。当然,PINN 也有收敛慢、梯度消失等挑战,比如,复杂 PDE 可能导致梯度传播困难,收敛慢(梯度消失)、于具有快速变化特征的系统,如冲击波,标准 PINN 可能精度不足,需要改进策略,比如自适应采样或多尺度特征提取。但随着研究深入,这些问题正在不断被解决。物理或化学方程为什么往往是偏微分方程?什么是物理信息神经网络(PINN)?x_%7Bi%7D。

qq_52635735的博客 1万+

数据结构-汉诺塔问题

汉诺塔 汉诺塔(Tower of Hanoi)源于印度传说中,大梵天创造世界时造了三根金钢石柱子,其中一根柱子自底向上叠着64片黄金圆盘。大梵天命令婆罗门把圆盘从下面开始按大小顺序重新摆放在另一根柱子上。并且规定,在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。 汉诺塔也是一个经典的递归问题。在线游玩 案例1:移动3个盘子最短路径: 案例2:移动4个盘子的最短路径: 当我们将前三个...

Hello World 1869

EndNote20保姆级安装教程:从下载到汉化一步到位(附闪退解决方案)

本文提供EndNote20的详细安装与汉化教程,涵盖系统准备、分步安装、路径优化及闪退解决方案。针对科研人员的需求,特别优化了中文环境下的使用体验,确保文献管理工具的高效稳定运行。

weixin_30415113的博客 341

求解汉诺塔问题(提示, 使用递归)

package com.test; import java.util.Scanner; public class Test { //主方法 public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("请输入汉诺塔的层数:"...

helloword233的博客 6780

星网锐捷语音网关密码重置全攻略:两种高效恢复出厂设置方法详解

本文详细解析了星网锐捷语音网关密码重置的两种高效方法,包括通过eth1通用口和eth0管理口的操作步骤。针对不同型号设备,提供硬件连接、Web控制台登录及CRT工具使用的具体指南,帮助用户快速恢复出厂设置并重建语音环境配置。

weixin_28698059的博客 528

基于FPGA的DDS信号发生器

之前的博客讲到了DDS的基本原理,现在用Verilog代码实现DDS,能够产生四种波形,方波,三角波,正弦波,锯齿波,用按键来控制频率和选择波形。其中按键消抖模块来自小梅哥的FPGA自学笔记设计与验证这本书。 module DDS_Module( Clk, Rst_n, EN, Fword, Key1, Pword, DA_Clk, DA_Data ); input Clk...

qq_43534451的博客 9950

数据结构11:汉诺塔问题

汉诺塔是一个经典递归问题,

yjsfl_xiang的专栏 819

SAP-ABAP:SAP中如何查看系统表之间的关联关系?

在业务逻辑开发或者ALV开发过程中,我们通常要从系统中找出几个标准表之间的关联关系,有的是head与item,通过外键进行关联的关系。那么怎么去找到呢?以上就是具体如何产看系统表之间的关联关系的步骤,对于不同代码的业务顾问,也是必须要掌握的。

LinkStar的博客 2430

数据结构-汉诺塔

汉诺塔又称河内塔问题是源于印度一个古老传说的益智玩具,创造世界的时候做了三根金刚石柱子,在一根柱子上从下往上按照大小顺序摞着64片黄金圆盘,把圆盘从下面开始按大小顺序重新摆放在另一根柱子上,并且规定在小圆盘上不能放大圆盘,在三根柱子之间一次只能移动一个圆盘。 对ABC三柱第一步是把前n-1个从A移到B,最后一次是把n移到C,第二步是把n-1个从B移到C,最后一次是把第1个移到C也就是终结条件 A->C A->B C->B A->C B-&gt...

aaa0329的博客 1344

【PointNet++】制作点云数据集使用CloudCompare进行标注

使用PointNet++复现成功后,需要训练自己的数据集。于是使用cloudcompare参照ShapeNet的数据集格式去制作,最终保存的txt文件格式为“xyz+rgb+label”,一共7列。 自己按此制作好的数据集已成功训练及推测,于是记录下来让好兄弟好姐妹们少踩坑多吃肉。

纵马踏花向自由 5715

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

【数据结构】汉诺塔问题

#include <stdio.h> //把n个盘子从x起始柱经由y过渡柱移动到z终点柱 void hanoi(int n, char x,char y,char z); //从x柱子移动到z柱子 void move(char x,char z); int main(){ int n; printf("请输入柱子的个数:\n"); scanf("%d",&n); hanoi(n,'a','b','c'); return 0; } vo

l634007854的博客 162

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

【数据结构】递归专题——汉诺塔详解

【递归专题】汉诺塔问题递归实现思路与代码

weixin_62533201的博客 3171

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【数据结构与算法】汉诺塔问题(C++)

本文的 C++ 非递归实现基于栈递归的核心思想,结合 C++ 面向对象和标准库的优势,实现了内存安全、无栈溢出、高效率、易维护的汉诺塔解法,相比 C 语言版和 C++ 递归版更适合实际工程应用。通过学习该实现,不仅能掌握汉诺塔的非递归解法,更能理解递归的本质(系统栈的自动调用)和递归转非递归的通用方法(自定义栈模拟状态),这一思路可推广到所有递归问题(如二叉树遍历、深度优先搜索 DFS、归并排序等)的 C++ 非递归实现中。执笔至此,感触彼多,全文将至,落笔为终。

闻道有先后,术业有专攻 1873

数据结构——汉诺塔问题

例如,n=3时,即hanoi(3, a, b, c)入栈,n=2时,hanoi(2, a, b, c)入栈,n=1时,hanoi(1, a, b, c)入栈,当n=0时,不满足条件,就开始出栈,首先hanoi(1, a, b, c)出栈,然后hanoi(2, a, b, c)出栈,最后hanoi(3, a, b, c)出栈.如汉诺塔问题,hanoi函数中的n,a,b,c都是形参,而我们在主函数中输入的('3', 'A', 'B', 'C')则是实参。汉诺塔问题中盘子的个数时n,那么空间复杂度为O(n)。

m0_73949658的博客 977

【数据结构|汉诺塔问题】汉诺塔问题以及递归思想实现

汉诺塔游戏分析

爱喝冰红茶的方舟的博客 2039

数据结构之汉诺塔问题

汉诺塔问题是一道经典的递归问题,通过研究汉诺塔问题,能够对递归有一个更深刻的理解。

m0_73305158的博客 179

数据结构学习——汉诺塔问题

文章目录前言——汉诺塔问题一、实际操作完整代码二、测试1.输入数字2.测试结果 前言——汉诺塔问题 汉诺塔问题,是心理学实验研究常用的任务之一。该问题的主要材料包括三根高度相同的柱子和一些大小及颜色不同的圆盘,三根柱子分别为起始柱A、辅助柱B及目标柱C。 一、实际操作 完整代码 #include <stdio.h> int i=1; void hanoi(int paraNumber , char paraSource , char paraDestination , char paraT

weixin_62410421的博客 846

【数据结构】基于递归策略解决汉诺塔问题及其变体

汉诺塔问题是一种经典的问题。常见的处理方法是使用递归策略来处理。下面我们来介绍汉诺塔问题,及其两种变体。

小兮的皮卡丘 875

数据结构之递归算法解决汉诺塔问题

递归算法解决汉诺塔问题

sunny_daily的博客 1304

数据结构--汉诺塔

经典的递归问题:汉诺塔 上代码: #include <stdio.h> #include <stdlib.h> int time=0; void move(int n,char start,char transfer,char objective){ time++; if (n==1){ printf("%c-->%c\n",start,objective); } else { move(n-1,start,

xiyandr的博客 670

数据结构3.4递归算法——累加问题、汉诺塔问题

递归指的是,一个函数不断引用自身,直到引用的唯一已知对象时止的过程。 一、对于累加问题的处理,通常有两种解决方法 1.利用for或者while循环 #include<stdio.h> int main() { int n;//add to n int i;//cyclic variable int sum=0;//total scanf_s("%d",&n); if(n<0){ printf("false");

qq_62644791的博客 734
上一篇: 链式队列【数据结构-朱战立】
下一篇: 计算递归次数【数据结构-朱战立】
蓝莓派Alex
博客等级 码龄15年 147粉丝 49原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值