#include <iostream>
using namespace std;
void Hanoi(char src, char des, char via, int n)
{
if(n == 1)
{
cout << n <<" : "<< src <<" --> " <<des << endl;
return;
}
Hanoi(src, via, des, n - 1);
cout << n <<" : "<< src <<" --> " <<des << endl;
Hanoi(via, des, src, n - 1);
}
int main()
{
int n;
cin >> n;
cout<<"recusive:"<< endl;
Hanoi('A','C','B', n);
cout << endl;
cout<<"normal:"<<endl;
char order[2][256];
char pos[64];
order[0]['A'] = 'B';
order[0]['B'] = 'C';
order[0]['C'] = 'A';
order[1]['A'] = 'C';
order[1]['B'] = 'A';
order[1]['C'] = 'B';
//0是顺序 1是逆序
int index[64];
//确定轨迹的顺序还是逆序
int i, j, m;
for(i = n; i > 0; i -= 2)
index[i] = 1;
for(i = n - 1; i > 0; i -= 2)
index[i] = 0;
memset(pos, 'A', sizeof(pos));
for(i = 1; i < (1 << n); i ++)
{
for(m = 1, j = i; j%2 == 0; j/=2, m ++);
cout << m <<" : "<< pos[m] <<" --> " << order[index[m]][pos[m]] << endl;
pos[m] = order[index[m]][pos[m]];
}
return 0;
}
2019-07-17 22:55:06