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優秀算法河內之塔

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377603.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377603.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377603.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

轉——C# DataGridView控件 動態添加新行

DataGridView控件在實際應用中非常實用,特別需要表格顯示數據時。可以靜態綁定數據源,這樣就自動為DataGridView控件添加相應的行。假如需要動態為DataGridView控件添加新行,方法有很多種,下面簡單介紹如何為DataGridView控件動態…

分享通用基類庫-C#通用緩存類

1 /************************************************************************************* 2 * 代碼:吳蔣 3 * 時間:2012.03.30 4 * 說明:緩存公共基類 5 * 其他: 6 * 修改人: 7 * 修改時間: 8 * 修改說明: 9 ******************…

二、PyTorch加載數據

一、常用的兩個函數 dir()函數可以理解為打開某個包,help()可以理解為返回如何使用某個具體的方法 例如:若一個A錢包里面有a,b,c,d四個小包,則可通過dir(A),打開該A錢包,返回a&…

leetcode 1005. K 次取反后最大化的數組和 思考分析

題目 給定一個整數數組 A,我們只能用以下方法修改該數組:我們選擇某個索引 i 并將 A[i] 替換為 -A[i],然后總共重復這個過程 K 次。(我們可以多次選擇同一個索引 i。) 以這種方式修改數組后,返回數組可能…

三、TensorBoard

一、安裝TensorBoard 管理員身份運行Anaconda Prompt,進入自己的環境環境 conda activate y_pytorch,pip install tensorboard 進行下載,也可以通過conda install tensorboard進行下載。其實通俗點,pip相當于菜市場,c…

IT資產管理系統SQL版

你難道還在用Excel登記IT資產信息嗎? 那你一定要好好考慮如何面對以下問題 1:IT人員需要面對自身部門以下問題用戶申請了資產it部未處理的單還有哪些?庫存里面還有哪些資產?有多少設備在維修?有多少設備已經報廢了?哪些資產低于安全庫存需要采購?使…

詳細講解設計跳表的三個步驟(查找、插入、刪除)

目錄寫在前面跳表概要查找步驟插入步驟刪除步驟完整代碼寫在前面 關于跳表的一些知識可以參考這篇文章,最好是先看完這篇文章再看詳細的思路->代碼的復現步驟: Redis內部數據結構詳解(6)——skiplist 關于跳表的插入、刪除基本操作其實也就是鏈表的插入和刪除,所…

php 類靜態變量 和 常量消耗內存及時間對比

在對類執行100w次循環后, 常量最快,變量其次,靜態變量消耗時間最高 其中: 常量消耗:101.1739毫秒 變量消耗:2039.7689毫秒 靜態變量消耗:4084.8911毫秒 測試代碼: class Timer_profi…

一個機器周期 計算機_計算機科學組織| 機器周期

一個機器周期 計算機機器周期 (Machine Cycle) The cycle during which a machine language instruction is executed by the processor of the computer system is known as the machine cycle. If a program contains 10 machine language instruction, 10 separate machine …

四、Transforms

transform是torchvision下的一個.py文件,這個python文件中定義了很多的類和方法,主要實現對圖片進行一些變換操作 一、Transforms講解 from torchvision import transforms#按著Ctrl,點擊transforms進入到__init__.py文件中 from .transfo…

leetcode 134. 加油站 思考分析

目錄題目1、暴力法,雙層遍歷2、貪心題目 在一條環路上有 N 個加油站,其中第 i 個加油站有汽油 gas[i] 升。 你有一輛油箱容量無限的的汽車,從第 i 個加油站開往第 i1 個加油站需要消耗汽油 cost[i] 升。你從其中的一個加油站出發&#xff0…

單鏈線性表的實現

//函數結果狀態代碼#define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 //Status是函數的類型,其值是函數結果狀態代碼 typedef int Status; typedef int ElemType;…

時間模塊,帶Python示例

Python時間模塊 (Python time Module) The time module is a built-in module in Python and it has various functions that require to perform more operations on time. This is one of the best modules in Python that used to solve various real-life time-related pro…

五、torchvision

一、下載CIFAR-10數據集 CIFAR-10數據集官網 通過閱讀官網給的解釋可以大概了解到,一共6w張圖片,每張圖片大小為3232,5w張訓練圖像,1w張測試圖像,一共由十大類圖像。 CIFAR10官網使用文檔 torchvision.datasets.CIF…

leetcode 69. x 的平方根 思考分析

題目 實現 int sqrt(int x) 函數。 計算并返回 x 的平方根,其中 x 是非負整數。 由于返回類型是整數,結果只保留整數的部分,小數部分將被舍去。 示例 1: 輸入: 4 輸出: 2 示例 2: 輸入: 8 輸出: 2 說明: 8 的平方根是 2.82842…, 由于返回…

背包問題 小灰_小背包問題

背包問題 小灰Prerequisites: Algorithm for fractional knapsack problem 先決條件: 分數背包問題算法 Here, we are discussing the practical implementation of the fractional knapsack problem. It can be solved using the greedy approach and in fraction…

360瀏覽器兼容問題

360瀏覽器兼容問題 360瀏覽器又是一大奇葩,市場份額大,讓我們不得不也對他做些兼容性處理。 360瀏覽器提供了兩種瀏覽模式,極速模式和兼容模式,極速模式下是webkit內核的處理模式,兼容模式下是與IE內核相同的處理模式。…

轉 設計師也需要了解的一些前端知識

一、常見視覺效果是如何實現的 一些事 關于文字效果 互聯網的一些事 文字自身屬性相關的效果css中都是有相對應的樣式的,如字號、行高、加粗、傾斜、下劃線等,但是一些特殊的效果,主要表現為ps中圖層樣式中的效果,css是無能為力的…

六、DataLoader

一、DataLoader參數解析 DataLoader官網使用手冊 參數描述dataset說明數據集所在的位置、數據總數等batch_size每次取多少張圖片shuffleTrue亂序、False順序(默認)samplerbatch_samplernum_workers多進程,默認為0采用主進程加載數據collate_fnpin_memorydrop_las…

單調棧 leetcode整理(一)

目錄單調棧知識402. 移掉K位數字1673. 找出最具競爭力的子序列316. 去除重復字母(1081. 不同字符的最小子序列)321. 拼接最大數單調棧知識 單調棧就是一個內部元素有序的棧(大->小 or 小->大),但是只用到它的一…