使用棧實現漢諾塔求解 C 原始碼

2021-10-08 20:35:37 字數 4346 閱讀 8313

漢諾塔的核心就是一句話,要把src的第n個碟子移動到dst,需要三步:

1,把n-1個碟子移動到buffer

2,把第n個碟子移動到dst

3,把buffer中的碟子移動到dst

實現的核心**如下:

if

(_n >0)

完整**如下:

#include

#include

#include

using

namespace std;

#define n 5

#define src 0

#define dst 1

#define buffer 2

stack<

int> stks[3]

;void

print_stack

(stack<

int>

& s)

while

(!tmp.

empty()

) std::cout<}void

move

(int _n,

int _src,

int _dst,

int _buffer)

}int

main

(int argc,

char

*ar**)

// move value

move

(n, src, dst, buffer)

;return0;

}

執行結果如下:

..

....

....

.....

src stack:

5 4 3 2

dst stack:

1buffer stack:

....

....

....

...src stack:

5 4 3

dst stack:

1buffer stack:2..

....

....

.....

src stack:

5 4 3

dst stack:

buffer stack:

2 1..

....

....

.....

src stack:

5 4dst stack:

3buffer stack:

2 1..

....

....

.....

src stack:

5 4 1

dst stack:

3buffer stack:2..

....

....

.....

src stack:

5 4 1

dst stack:

3 2buffer stack:

....

....

....

...src stack:

5 4dst stack:

3 2 1

buffer stack:

....

....

....

...src stack:

5dst stack:

3 2 1

buffer stack:4..

....

....

.....

src stack:

5dst stack:

3 2buffer stack:

4 1..

....

....

.....

src stack:

5 2dst stack:

3buffer stack:

4 1..

....

....

.....

src stack:

5 2 1

dst stack:

3buffer stack:4..

....

....

.....

src stack:

5 2 1

dst stack:

buffer stack:

4 3..

....

....

.....

src stack:

5 2dst stack:

1buffer stack:

4 3..

....

....

.....

src stack:

5dst stack:

1buffer stack:

4 3 2

....

....

....

...src stack:

5dst stack:

buffer stack:

4 3 2 1

....

....

....

...src stack:

dst stack:

5buffer stack:

4 3 2 1

....

....

....

...src stack:

1dst stack:

5buffer stack:

4 3 2

....

....

....

...src stack:

1dst stack:

5 2buffer stack:

4 3..

....

....

.....

src stack:

dst stack:

5 2 1

buffer stack:

4 3..

....

....

.....

src stack:

3dst stack:

5 2 1

buffer stack:4..

....

....

.....

src stack:

3dst stack:

5 2buffer stack:

4 1..

....

....

.....

src stack:

3 2dst stack:

5buffer stack:

4 1..

....

....

.....

src stack:

3 2 1

dst stack:

5buffer stack:4..

....

....

.....

src stack:

3 2 1

dst stack:

5 4buffer stack:

....

....

....

...src stack:

3 2dst stack:

5 4 1

buffer stack:

....

....

....

...src stack:

3dst stack:

5 4 1

buffer stack:2..

....

....

.....

src stack:

3dst stack:

5 4buffer stack:

2 1..

....

....

.....

src stack:

dst stack:

5 4 3

buffer stack:

2 1..

....

....

.....

src stack:

1dst stack:

5 4 3

buffer stack:2..

....

....

.....

src stack:

1dst stack:

5 4 3 2

buffer stack:

....

....

....

...src stack:

dst stack:

5 4 3 2 1

buffer stack:

用棧來求解漢諾塔問題

漢諾塔問題比較經典,這裡修改一下遊戲規則 現在限制不能從最左側的塔直接移動到最右側,也不能從最右側直接移動到最左側,而是必須經過中間。求當塔有n層的時候,列印最優移動過程和最優移動總步數。輸入描述 輸入乙個數n,表示塔層數 輸出描述 按樣例格式輸出最優移動過程和最優移動總步數 示例1輸入2輸出mov...

用棧來求解漢諾塔問題

說明 本文是左程雲老師所著的 程式設計師面試 指南 第一章中 用棧來實現漢諾塔問題 這一題目的c 棧方法的復現。感謝左程雲老師的支援。題目 漢諾塔問題比較經典,這裡修改一下遊戲規則 現在限制不能從最左側的塔直接移動到最右側,也不能從最右側直接移動到最左側,而必須經過中間。求當有n層塔的時候,列印最優...

漢諾塔c 實現

用c 實現了漢諾塔,使用了遞迴,使用了上一次實現的堆疊這個資料結構來實現的漢諾塔,下面是 stack.h main.cpp stackdemo created by xin wang on 4 15 15.include class outofbounds 刪除定義的陣列 bool isempty ...