java优秀算法河内之塔_河内塔的Java程序

java优秀算法河内之塔

Tower of Hanoi is a mathematical puzzle where we have three rods and n disks. The objective of the puzzle is to move all disks from source rod to destination rod using the third rod (say auxiliary). The rules are:

河内塔是一个数学难题,其中我们有三个杆和n个盘。 难题的目的是使用第三个杆(例如辅助杆)将所有磁盘从源杆移动到目标杆。 规则是:

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

    一次只能移动一个磁盘。

  2. A disk can be moved only if it is on the top of a rod.

    仅当磁盘位于杆的顶部时才能移动磁盘。

  3. No disk can be placed on the top of a smaller disk.

    不能将磁盘放置在较小磁盘的顶部。

Print the steps required to move n disks from source rod to destination rod. Source Rod is named as 'a', auxiliary rod as 'b' and destination rod as 'c'.

打印将n个磁盘从源棒移到目标棒所需的步骤。 源杆称为'a' ,辅助杆称为'b' ,目的杆称为'c'

Input format: Integer n

输入格式:整数n

Output format: Steps in different lines (in one line print source and destination rod name separated by space).

输出格式:不同行中的步骤(在一行中,打印源和目标杆名称用空格分隔)。

Example:

例:

    Sample Input:
    2

    Sample Output:
    a b
    a c
    b c

Explanation:

说明:

This is one of the famous problems on recursion. To solve this, let's assume the steps taken for 2 disks. Let’s assume Rod A, B, and C and we have to shift the disks from A to B. First, we shift the smaller disk to C, then we shift the larger disk to B, and at last, put the smaller disk from C to B.

这是递归的著名问题之一。 为了解决这个问题,我们假设对2个磁盘采取了步骤。 假设杆A,B和C,我们必须将磁盘从A移到B。首先,将较小的磁盘移至C,然后将较大的磁盘移至B,最后,将较小的磁盘从C移入到B。

Therefore, for N disks, lets recursion shifts N-1 disks to C, and we will shift the last disk to B and again let recursion shifts rest of the disk to C. Using this, we will be able to solve the problem.

因此,对于N个磁盘,让递归将N-1个磁盘移至C,然后将最后一个磁盘移至B,再让递归将其余磁盘移至C。使用此方法,我们可以解决问题。

Algorithm:

算法:

Declare a recursive function towerOfHanoi with parameters (int disk, char source, char auxiliary, char destination)

声明带有参数(int磁盘,char源,char辅助,char目标)的递归函数towerOfHanoi

  • STEP 1: Base Case: If(disk == 0) return;

    步骤1:基本情况: If(disk == 0)返回;

  • STEP 2: Base Case: If(disk == 1) Print Source to Destination

    步骤2:基本情况: If(disk == 1)打印源到目标

  • STEP 3: Recursive Case: towerOfHanoi(disk -1, source, destination, auxiliary)

    步骤3:递归案例: towerOfHanoi(磁盘-1,源,目标,辅助)

  • STEP 4: Print Source to Destination

    步骤4:将源打印到目标

  • STEP 5: towerOfHanoi(disk -1, auxiliary, source,destination)

    步骤5: TowerOfHanoi(磁盘-1,辅助,源,目标)

Example:

例:

    Input : 3

    Disk 1 moved from A to C
    Disk 2 moved from A to B
    Disk 1 moved from C to B
    Disk 3 moved from A to C
    Disk 1 moved from B to A
    Disk 2 moved from B to C
    Disk 1 moved from A to C


Program:

程序:

import java.util.Scanner;

public class Main {
     
	//Recursive Function
	public static void towerOfHanoi(int disks, char source, char auxiliary, char destination) {
      
		// Write your code here
		if(disks==0){
        //Base Case 1
			return;
		}
		if(disks==1){
        //Base Case 2
			System.out.println(source +" " + destination);
			return;
		}
		else{
     
			//Shifting d-1 disk from A to C
			towerOfHanoi(disks-1,source,destination,auxiliary);   
			System.out.println(source + " " + destination);
			//Shifting d-1 disk from c to B
			towerOfHanoi(disks-1,auxiliary,source,destination);   
		}
	}

	public static void main(String[] args) {
     
		int disk;
		Scanner s = new Scanner(System.in);
		
		System.out.print("Print No of Disks: ");
		disk = s.nextInt();
		
		towerOfHanoi(disk, 'A', 'C', 'B');
	}
}

Output

输出量

Print No of Disks: 3
A B
A C
B C
A B
C A
C B
A B


翻译自: https://www.includehelp.com/java-programs/tower-of-hanoi.aspx

java优秀算法河内之塔

你可能感兴趣的:(java,算法,python,深度学习,linux)