//hnt.cpp, the solution for hnt question using stacks
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include <stack>
#include <ctime>
class Cplat
{
public:
Cplat(short ibottom=0, short itop=0):bottom(ibottom), top(itop)
{
}
short bottom;
short top;
};
class Caction
{
public:
Caction(short ibottom=0, short itop =0, short isrc=0, short ides=0):
plat(ibottom, itop), src(isrc), des(ides)
{
}
short src: 2;
short des: 2;
Cplat plat;
};
void display(short plat, short src, short des);
typedef std::stack < Caction > psT;//std::, remember
int main()
{
clock_t start=clock();
short number;
Caction action;
psT ps;
//cout<<"the src is A, the min is B, the des is C"<
//cout<<"consider you want to move plats from A to C"<
//cout<<"how much plats you want to move?"<
cin>>number;
ps.push(Caction(1, number, 0, 2));
while(!ps.empty())
{
action=ps.top();
ps.pop();//reduce the top action
if(action.plat.bottom==action.plat.top)//a action only moves one plat, it can be simly finished
{
display(action.plat.bottom, action.src, action.des);
}
else//a action moves more than one plat
{
ps.push( Caction(action.plat.bottom+1, action.plat.top, ~(action.src ^ action.des), action.des));
ps.push( Caction(action.plat.bottom, action.plat.bottom, action.src, action.des));
ps.push( Caction(action.plat.bottom+1, action.plat.top, action.src, ~(action.src ^ action.des)));
}
}
clock_t finish=clock();
cout<<"the eclapsed time is "<<static_cast<double>(finish-start)/CLOCKS_PER_SEC;
}
void display(short plat, short src, short des)
{
char c_src=src==0?'A':src==1?'B':'C';
char c_des=des==0?'A':des==1?'B':'C';
//cout<<"move plat "<
}
|