当前位置:文档之家› 汉诺塔c++程序

汉诺塔c++程序

if (platesCount == 1) {
listSavedState.push_back(platesCount); listSavedState.push_back(source); listSavedState.push_back(destination); listSavedState.push_back(intermediate); return ; } else {
Hanoi(platesCount -1, from, by, dest); Hanoi(1, from, dest, by); Hanoi(platesCount -1, by, dest, from); } } // Advance one step to solve Hanoivoid HanoiDrawer::SolveNextStep() { int platesCount , source , destination , intermediate;
Hanoi(platesCount - 1, source, intermediate, destination); Hanoi(1, source, destination, intermediate); Hanoi(platesCount - 1, intermediate, destination, source); return ; } }
if (listSavedState.size()==0) {
this->Hanoi(this->iPlatesCount, HanoiDrawer::SOURCE , HanoiDrawer::DESTINATION,
HanoiDrawer::INTERMEDIATE); }
if(listSavedState.size() % 4 != 0 ) {
SetDlgItemText(this->hWnd, this->toContainerResourceId , PlaceIntToString(destination).c_str() );
SetDlgItemText(this->hWnd, this->throughContainerResourceId , PlaceIntቤተ መጻሕፍቲ ባይዱoString(intermediate).c_str() );
this->solved = true; }
SetDlgItemInt(this->hWnd, this->countContainerResourceId, GetMovesCount(), FALSE);
SetDlgItemText(this->hWnd, this->fromContainerResourceId, PlaceIntToString(source).c_str() );
void Hanoi(int platesCount, int from, int dest, int by) {
if (platesCount==1) {
printf( "Move the plate from %d to %d through %d" , from, dest, by);
} else {
} // Draws stands and plates// then do Invalidate// this operation is required// in each stepvoid HanoiDrawer::ReDraw() {
DrawStands();
DrawPlates();
Invalidate(); } // The internal function that is responsible// about solve the problem.// platesCount : how many plates// source : the index of the source// destination : the index of the destination// intermediate : the index of the intermediatevoid HanoiDrawer::Hanoi(int platesCount, int source, int destination, int intermediate) {
intermediate = listSavedState.front(); listSavedState.pop_front();
MovePlateFromTo(source, destination);
this - > iMovesCount++;
if(iMovesCount == this->GetTotalMovesCount()) {
return ; }
platesCount = listSavedState.front(); listSavedState.pop_front();
source = listSavedState.front(); listSavedState.pop_front();
destination = listSavedState.front(); listSavedState.pop_front();
相关主题