#!/bin/bash - #=============================================================================== # # FILE: Hanoi_tower.sh # # USAGE: ./Hanoi_tower.sh # # DESCRIPTION: # # OPTIONS: --- # REQUIREMENTS: --- # BUGS: --- # NOTES: --- # AUTHOR: Francis Drake K (), 772523354@qq.com # ORGANIZATION: M33 # CREATED: 2018年08月16日 20:04 # REVISION: --- #=============================================================================== tower_1=() tower_2=() tower_3=() step=(0) level_up=() create_tower () { for (( CNTR=0; CNTR<$1; CNTR+=1 )); do tower_1=( ${tower_1[*]} `echo "$CNTR+1" |bc ` ) done } # ---------- end of function create_tower ---------- #--- FUNCTION ---------------------------------------------------------------- # NAME: mv_to_tower3 # DESCRIPTION: 將標準塔移動到tower3 # PARAMETERS: tower_name() # RETURNS: #------------------------------------------------------------------------------- mv_to_tower3 () { if [ ${tower_3[*]} -ne 0 ] ; then # 若是塔中有元素則更新目標塔 update_to_tower fi # 將參數數組(起始塔)的最上層元素(數組的下標0位置的數據)賦值給塔3的最上層元素 tower_3[0]= ${$1[0]} # 刪除參數數組(起始塔)的最上層元素 unset ${$1[0]} # 更新起始塔(將數組中的所有元素所有下標-1) update_from_tower $1 # 將元素,起始塔,目標塔當作參數傳入count_step count_step ${$1[0]} $1 tower_3 } # ---------- end of function mv_to_tower3 ---------- mv_to_tower2 () { if [ ${tower_2[*]} -ne 0 ] ; then # 若是塔中有元素則更新目標塔 update_to_tower fi # 將參數數組(起始塔)的最上層元素(數組的下標0位置的數據)賦值給塔3的最上層元素 tower_2[0]= ${$1[0]} # 刪除參數數組(起始塔)的最上層元素 unset ${$1[0]} # 更新起始塔(將數組中的所有元素所有下標-1) update_from_tower $1 # 將元素,起始塔,目標塔當作參數傳入count_step count_step ${$1[0]} $1 tower_2 } # ---------- end of function mv_to_tower2 ---------- mv_to_tower1 () { if [ ${tower_1[*]} -ne 0 ] ; then # 若是塔中有元素則更新目標塔 update_to_tower fi # 將參數數組(起始塔)的最上層元素(數組的下標0位置的數據)賦值給塔3的最上層元素 tower_1[0]= ${$1[0]} # 刪除參數數組(起始塔)的最上層元素 unset ${$1[0]} # 更新起始塔(將數組中的所有元素所有下標-1) update_from_tower $1 # 將元素,起始塔,目標塔當作參數傳入count_step count_step ${$1[0]} $1 tower_2 } # ---------- end of function mv_to_tower1 ---------- #--- FUNCTION ---------------------------------------------------------------- # NAME: update_tower # DESCRIPTION: 更新起始塔(數組) # PARAMETERS: tower_name # RETURNS: #------------------------------------------------------------------------------- update_from_tower () { for (( CNTR=1; CNTR<${$1[*]}; CNTR+=1 )); do ${$1[$CNTR-1]}= ${$1[$CNTR]} done } # ---------- end of function update_tower ---------- #--- FUNCTION ---------------------------------------------------------------- # NAME: update_to_tower # DESCRIPTION: 更新目標塔(數組) # PARAMETERS: tower——name # RETURNS: #------------------------------------------------------------------------------- update_to_tower () { for (( CNTR=0; CNTR<${$1[*]}; CNTR+=1 )); do ${$1[$CNTR+1]}= ${$1[$CNTR]} unset ${$1[0]} done } # ---------- end of function update_to_tower ---------- #--- FUNCTION ---------------------------------------------------------------- # NAME: count_step # DESCRIPTION: 記錄起始塔,移動數,目標塔到step()中 # PARAMETERS: # RETURNS: #------------------------------------------------------------------------------- count_step () { lev=`echo "L$1"` tower_from=$2 tower_to=$3 step=(${step[*]} `echo "$lev $tower_from $tower_to"` ) } # ---------- end of function count_step ---------- #--- FUNCTION ---------------------------------------------------------------- # NAME: 3_level_tower_to # DESCRIPTION: 初始化level_up()將三階漢諾塔從塔1移動到塔3的方法存入 # PARAMETERS: # RETURNS: #------------------------------------------------------------------------------- 3_level_tower_to () { level_up=(${level_up[*]} echo 'mv_to_tower3 tower_1') # L1從塔1移動到塔3 level_up=(${level_up[*]} echo 'mv_to_tower2 tower_1') # L2從塔1移動到塔2 level_up=(${level_up[*]} echo ' mv_to_tower2 tower_3') # L1從塔1移動到塔2 level_up=(${level_up[*]} echo 'mv_to_tower3 tower_3') # L3從塔1移動到塔3 level_up=(${level_up[*]} echo 'mv_to_tower1 tower_2') # L1從塔1移動到塔1 level_up=(${level_up[*]} echo 'mv_to_tower3 tower_2') # L2從塔1移動到塔3 level_up=(${level_up[*]} echo 'mv_to_tower3 tower_1') # L1從塔1移動到塔3 }# ---------- end of function 3_level_tower_to ---------- #--- FUNCTION ---------------------------------------------------------------- # NAME: 3_level_tower_back # DESCRIPTION: 三階漢諾塔從塔3移動到塔1 # PARAMETERS: # RETURNS: #------------------------------------------------------------------------------- # 3_level_tower_back () # { # mv_to_tower1 tower_3 # L1從塔3移動到塔1 # mv_to_tower2 tower_3 # L2從塔3移動到塔2 # mv_to_tower2 tower_1 # L1從塔1移動到塔2 # mv_to_tower1 tower_3 # L3從塔3移動到塔1 # mv_to_tower3 tower_2 # L1從塔2移動到塔3 # mv_to_tower1 tower_2 # L2從塔2移動到塔1 # mv_to_tower1 tower_3 # L1從塔3移動到塔1 # } # ---------- end of function 3_level_tower_back ---------- #--- FUNCTION ---------------------------------------------------------------- # NAME: change_num # DESCRIPTION: 對換角標,例:參數 mv_to_tower1 tower3 可返回結果mv_to_tower3 tower1 # PARAMETERS: # RETURNS: #------------------------------------------------------------------------------- change_num () { way_num_tmp=` echo $1 |rev |cut -c1 ` change_from=` echo $2 |rev |cut -c1 ` way_output= ` echo "mv_to_tower"$change_from ` from_output=` echo "tower"$way_num_tmp level_up=(${level_up[*]} `echo "$way_output $from_output"` ) } # ---------- end of function change_num ---------- #--- FUNCTION ---------------------------------------------------------------- # NAME: tower_level_up # DESCRIPTION: 塔的移動方法隨階數的增加自動升級,並將其存入level_up() # PARAMETERS: # RETURNS: #------------------------------------------------------------------------------- tower_level_up () { #------------------------------------------------------------------------------- # 核心算法 漢諾塔學習升級 # 若是要求n階漢諾塔:應先求3階漢諾塔 # 經過三階漢諾塔爲基礎進行變換 # 例:4階漢諾塔的移動方法 # 3階漢諾塔正向移動 + 第四階移動到塔2 # +3階漢諾塔反向移動 + 第四階移動到塔3 # +3階漢諾塔正向移動 # 由此可知若已有4階漢諾塔的移動方法可求5階移動方法 # 因此已知3階漢諾塔可求n階漢諾塔 # 但因爲時間複雜度的提高和數據的溢出 # 不可求太高階的漢諾塔 # #------------------------------------------------------------------------------- # 由於數組中存入了3階漢諾塔的正向移動方法因此無需改變 for (( CNTR=3; CNTR<$1; CNTR+=1 )); do # 添加步驟 將高1階的塔從塔1移動到塔2 level_up=(${level_up[*]} echo "mv_to_tower2 tower_1" # 防止計算錯錯誤鎖定低階漢諾塔的正向移動方法的數組位置 arry_end=echo "`echo ${#level_up[@]}-2 |bc`" for (( i=0; i<$arry_end; i+=1 )); do # 生成反向移動,並存入數組 change_num ${level_up[$i]} done # 添加步驟 將高一階的塔從塔2移動到塔3 level_up=(${level_up[*]} echo"mv_to_tower3 tower_2" # 再次使用低階正向移動方法 for (( j=0; j<$arry_end; CNTR+=1 )); do level_up=(${level_up[*]} `echo ${level_up[j]}`) done done } # ---------- end of function tower_level_up ---------- main () {: # 輸入一個要完成的階數 # 生成數組 # create_tower # 傳入tower_level_up # 得出目標方法 # 運行目標方法 # 輸出countstep內容 # 便可得到移動步驟和最終塔 } # ---------- end of function main ----------