河内塔递归_使用递归的河内塔问题的C程序

河内塔递归

Here you will get C program for tower of hanoi problem using recursion.

在这里,您将获得使用递归的河内塔问题的C程序。

The Tower of Hanoi (also called the Tower of Brahma or Lucas’ Tower and sometimes pluralized) is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape as shown in below image.

河内塔(又称梵天塔或卢卡斯塔,有时甚至是复数塔)是一种数学游戏或益智游戏。 它由三个杆和许多可滑动到任何杆上的不同大小的磁盘组成。 难题始于盘片在一个杆上的排列顺序按大小的升序排列,顶部最小,从而形成圆锥形,如下图所示。

C Program for Tower of Hanoi Problem

The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:

难题的目的是遵循以下简单规则将整个堆栈移动到另一个杆:

  1. Only one disk can be moved at a time.

    一次只能移动一个磁盘。
  2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.

    每次移动都包括从一个堆栈中取出较高的磁盘并将其放在另一个堆栈的顶部,即,只有在磁盘是堆栈中最上面的磁盘时才能移动磁盘。
  3. No disk may be placed on top of a smaller disk.

    请勿在较小的磁盘上放置任何磁盘。

With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n-1, where n is the number of disks.

使用三个磁盘,可以通过七个步骤解决难题。 解决“河内之塔”难题所需的最小移动数为2n-1,其中n是磁盘数。

Animated solution of the Tower of Hanoi Puzzle for T(4,3)

河内拼图之塔T(4,3)的动画解决方案

Tower of Hanoi Problem Animation

C Program using recursion is given below which finds solution for Tower of Hanoi Problem.

下面给出了使用递归的C程序 ,它找到了河内问题塔的解决方案。

使用递归的河内塔问题的C程序 (C Program for Tower of Hanoi Problem Using Recursion)

#include<stdio.h>
 
void TOH(int,char,char,char);
 
void main()
{
	int n;
	printf("How many plates?");
	scanf("%d",&n);
	TOH(n,'A','B','C');
}
 
void TOH(int n,char x,char y,char z)
{
	if(n>0)
	{
		TOH(n-1,x,z,y);
		printf("\n%c -> %c",x,y);
		TOH(n-1,z,y,x);
	}
}

Output

输出量

How many plates?3

几盘?3

A -> B A -> C B -> C A -> B C -> A C -> B A -> B

A-> B A-> C B-> C A-> B C-> A C-> B A-> B

Source: https://en.wikipedia.org/wiki/Tower_of_Hanoi

资料来源: https : //en.wikipedia.org/wiki/Tower_of_Hanoi

Comment below if you have doubts or found anything incorrect related to above program for tower of hanoi in C.

如果您有疑问或发现与上述C河内塔楼程序有关的不正确之处,请在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2014/02/c-program-for-tower-of-hanoi-problem.html

河内塔递归

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值