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個盤。 難題的目的是使用第三個桿(例如輔助桿)將所有磁盤從源桿移動到目標桿。 規則是:
Only one disk can be moved at a time.
一次只能移動一個磁盤。
A disk can be moved only if it is on the top of a rod.
僅當磁盤位于桿的頂部時才能移動磁盤。
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優秀算法河內之塔