河內塔 簡易的 非遞迴解
有別於普通利用數學找出規律的方式
僅僅只有3個流程1個口訣,即可解出來
- [組牌]
- [發牌]
- [收牌或換牌]
有實驗過花幾分鐘記住口訣或看懂口訣,即便完全沒接觸的人
也能夠輕易解出多層的河內塔,當初實驗的人還蠻驚訝的XDD
代碼有些亂,有打算打掉重來全部套C++來寫
核心流程:
#include <iostream>
#include <stdio.h>
#include "fun\hanoi.h"
#include "fun\doulink.h"
using namespace std;
int main(int argc, char const *argv[]){
node** ht = new node*[3];
ht[0] = node_creat(-1);
ht[1] = node_creat(-1);
ht[2] = node_creat(-1);
int len = 8;
int *data = new int[len];
for (int i = 0; i < len; ++i)
data[i] = len-i;
nodep_input(ht[0], data, len);
int setup=2;
for (int i = 0; i < len-1; ++i){
setup*=2;
}setup--;
for (int i = 0; i < 1; ++i){
cout << "-------------------[" << i << "]" << "" << endl;
cout << "上一步: ";hanoi_logpri(ht);
if (hanoi_handdif(ht)==1){
if (hanoi_defrag(ht) == 0){
cout << " def" << endl;
continue;
}
}
if (hanoi_checksent(ht)==1){
if (hanoi_sent(ht) == 0){
cout << " sen" << endl;
continue;
}
}
if (hanoi_handdif(ht) % 2 == 0){
if (hanoi_receive(ht, 0) == 0){
cout << " sre" << endl;
continue;
}
else{
hanoi_receive(ht, 1);
cout << " bre" << endl;
continue;
}
}
else{
if (hanoi_change(ht)==0){
cout << " chg" << endl;
continue;
}
else{
hanoi_receive(ht, 1);
cout << " bre" << endl;
continue;
}
}
cout << "Nothing" << endl;
}
cout << "-------------------[" << setup << "]" << "" << endl;
cout << "上一步: ";hanoi_logpri(ht);
cout << "==============================================" <<endl;
hanoi_pri(ht);
for (int i = 2; i >= 0; --i){
node_deleteall(ht[i]);
delete [] ht[i];
} delete [] ht;
return 0;
}