網(wǎng)站首頁 編程語言 正文
簡介
漢諾塔問題是學(xué)數(shù)據(jù)結(jié)構(gòu)與算法的時(shí)候會遇到的問題,相信來看本文的讀者應(yīng)該都對漢諾塔問題有基本的了解,理論上所有的遞歸都可以改成循環(huán),常規(guī)的做法是借助堆棧,但是我一想好像用循環(huán)加數(shù)組也可以實(shí)現(xiàn),于是就有了本文,實(shí)現(xiàn)聲明,本文最后出來的算法效率不高的,比直接用遞歸實(shí)現(xiàn)還要差很多,追求算法效率的同學(xué)就不用看這個(gè)了。
題目:假設(shè)有3個(gè)柱子,分別為A、B、C,A柱子上有數(shù)量為n個(gè)的空心圓盤,從上到下序號分別為1...n,要求把A柱子中的n個(gè)圓盤移動到C柱中,且序號大的盤子必須在在需要小的圓盤下方。
思路:如果只有1個(gè)圓盤需要移動,則直接從A柱移動到C柱,如果有n個(gè)圓盤(n>1)需要移動,則需要先把n-1個(gè)圓盤從A柱移動到B柱,再把第n個(gè)圓盤從A柱移動到C柱,最后把原來的n-1個(gè)圓盤從B柱移動到C柱。
例如:
將1個(gè)圓盤從A柱移動到C柱只需要1個(gè)步驟:
1. 把圓盤1從A移動到C
將2個(gè)圓盤從A柱移動到C柱需要3個(gè)步驟:
1. 把圓盤1從A移動到B
2. 把圓盤2從A移動到C
3. 把圓盤1從B移動到C
將3個(gè)圓盤從A柱移動到C柱需要7個(gè)步驟:
1. 把圓盤1從A移動到C
2. 把圓盤2從A移動到B
3. 把圓盤1從C移動到B
4. 把圓盤3從A移動到C
5. 把圓盤1從B移動到A
6. 把圓盤2從B移動到C
7. 把圓盤1從A移動到C
遞歸的漢諾塔解法(c語言)
可以看出下面的遞歸算法的時(shí)間復(fù)雜度為O(2^n),因?yàn)榇嬖谟姓{(diào)用2^n-2次遞歸調(diào)用和1次原生printf;而空間復(fù)雜度為O(n),因?yàn)檫\(yùn)行時(shí)棧中最多會同時(shí)保存n個(gè)函數(shù)的調(diào)用信息。
#include <stdio.h> #include <stdlib.h> #include <math.h> void towers(int n, char from, char to, char aux); int main() { ?? ?towers(3, 'A', 'C', 'B'); ?? ?return 0; } void showMove (int order, char from, char to) { ?? ?printf("move disk %d from %c to %c\n", order, from, to); } void towers(int n, char from, char to, char aux) { ?? ?if (n==1) { ?? ??? ?showMove(n, from, to); ?? ??? ?return; ?? ?} ?? ?towers(n-1, from, aux, to); ? ? ? ? showMove(n, from, to); ?? ?towers(n-1, aux, to, from); }
解釋一下代碼中參數(shù)的含義,void towers(int n, char from, char to, char aux)的意思是把n個(gè)圓盤從from柱子移動到to柱子,中間可以借用aux柱子。
分析一下上面的遞歸執(zhí)行過程:
我們已經(jīng)知道把1個(gè)圓盤從from移動到to的步驟是showMove(1, from, to)
;
而把2個(gè)圓盤從from移動到to的步驟是,先照著移動1個(gè)圓盤的操作,把前面1個(gè)圓盤從from移動到aux,再把第2個(gè)圓盤從from移動到to,最后按照移動1個(gè)圓盤的操作,把剛才的1個(gè)圓盤從aux移動到to。
同理,把3個(gè)圓盤從from移動到to的步驟是,先照著移動2個(gè)圓盤的操作,把前面2個(gè)圓盤從from移動到aux,再把第3個(gè)圓盤從from移動到to,最后按照移動2個(gè)圓盤的操作,把剛才的2個(gè)圓盤從aux移動到to。
所以,把n個(gè)圓盤的操作從from移動到to的步驟是,先照著移動n-1個(gè)圓盤的操作,把前面n-1個(gè)圓盤從from移動到aux,再把第n個(gè)圓盤從from移動到to,最后按照移動n-1個(gè)圓盤的操作,把剛才的n-1個(gè)圓盤從aux移動到to。
因此,我們可以記錄移動1個(gè)圓盤的步驟,來得到移動2個(gè)圓盤的步驟,通過移動2個(gè)圓盤的步驟來得到移動3個(gè)圓盤的步驟,...最后得到移動n個(gè)圓盤的步驟。經(jīng)過分析可以知道移動n個(gè)圓盤一共會有2^n-1個(gè)步驟
循環(huán)實(shí)現(xiàn)漢諾塔問題(c語言)
為了代碼更加易懂,這里寫了注釋,修改了部分變量命名,統(tǒng)一用數(shù)組保存步驟集合,最后才輸出。
可以看出這個(gè)算法的時(shí)間復(fù)雜度還是O(2^n),一共會執(zhí)行2^(n+1)-1次setMoveAction語句和2^n-1次,printf語句,比起直接用遞歸還要復(fù)雜一些,而空間復(fù)雜度也是O(2^n),屬于沒什么用的算法,就是隨便寫寫。
#include <stdio.h> #include <stdlib.h> #include <math.h> void towers(int n, char from, char to, char aux); int main() { ?? ?towers(3, 'A', 'C', 'B'); ?? ?return 0; } void showMove(int order, char from, char to) { ?? ?printf("move disk %d from %c to %c\n", order, from, to); } typedef struct { ?? ?int order; ?? ?char from; ?? ?char to; } MoveAction; void setMoveAction(MoveAction *p, int order, char from, char to) { ?? ?p->order = order; ?? ?p->from = from; ?? ?p->to = to; } char compare_map(char c, char left, char right) { ?? ?if (c == left) { ?? ??? ?return right; ?? ?} else if (c == right) { ?? ??? ?return left; ?? ?} ?? ?return c; } void towers(int n, char from, char to, char aux) { ?? ?MoveAction *actions, action; ?? ?int i, k, size; ?? ?char f, t; ?? ?actions = (MoveAction *)calloc(pow(2, n - 1) - 1, sizeof(MoveAction)); ?? ?setMoveAction(&actions[0], 1, from, to); ?? ?size = 1; ?? ?for (i = 2; i <= n; i++) ?? ?{ ?? ??? ?//本次循環(huán)會將把i個(gè)盤子從from移動到to的步驟記錄到actions數(shù)組中 ?? ??? ?// 設(shè)size是把i-1個(gè)盤子從from移動到to的步驟數(shù), ?? ??? ?// 則本次循環(huán)中集合{actions[x] | 0 <= x <= size -1 }就是size是把i-1個(gè)盤子從from移動到aux的步驟集合, ?? ??? ?//而action[size]是把第i個(gè)盤子從from移到to, ?? ??? ?//而集合{actions[x] | size + 1 <= x <= 2*size }就應(yīng)該是把i-1個(gè)盤存從aux移動到to的步驟集合 ?? ??? ?// 倒序,先求解集合{actions[x] | size + 1 <= x <= 2*size } ?? ??? ?for (k = 0; k < size; k++) ?? ??? ?{ ?? ??? ??? ?action = actions[k]; ?? ??? ??? ?f = compare_map(action.from, from, aux); ?? ??? ??? ?t = compare_map(action.to, from, aux); ?? ??? ??? ?setMoveAction(&actions[k + size + 1], action.order, f, t); ?? ??? ?} ?? ??? ?// 求解action[size] ?? ??? ?setMoveAction(&actions[size], i, from, to); ?? ??? ?// 求解集合{actions[x] | 0 <= x <= size -1 } ?? ??? ?for (k = 0; k < size; k++) ?? ??? ?{ ?? ??? ??? ?action = actions[k]; ?? ??? ??? ?f = compare_map(action.from, to, aux); ?? ??? ??? ?t = compare_map(action.to, to, aux); ?? ??? ??? ?setMoveAction(&actions[k], action.order, f, t); ?? ??? ?} ?? ??? ?size = size * 2 + 1; ?? ?} ?? ?for (i = 0; i < size; i++) ?? ?{ ?? ??? ?showMove(actions[i].order, actions[i].from, actions[i].to); ?? ?} }
原文鏈接:https://juejin.cn/post/7057423612227092511
相關(guān)推薦
- 2022-06-19 C#中的checksum計(jì)算公式_C#教程
- 2022-12-05 python實(shí)現(xiàn)兩字符串映射_python
- 2022-07-14 Android檢測手機(jī)多點(diǎn)觸摸點(diǎn)數(shù)的方法_Android
- 2022-04-19 基于HarmonyOS 的ArkUI編寫的社區(qū)類app(一)----隱私服務(wù)條款界面
- 2023-01-05 Flutter?Widget之NavigationBar使用詳解_Android
- 2022-09-16 Python?docx庫刪除復(fù)制paragraph及行高設(shè)置圖片插入示例_python
- 2022-07-18 Redis服務(wù)器連接本地Linux所踩的坑
- 2023-02-06 Python?tkinter庫實(shí)現(xiàn)登錄注冊基本功能_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支