試圖解決河內的塔,我真的不明白爲什麼我的功能無法正常工作。我已經在這裏檢查了每個C++解決方案。用10塊板解決河內塔遞歸C++
bool CPlayer::MoveTop(int n, int from, int to)
{
if (n == 0)
{
m_towers.MoveDisc(to, 1);
return true;
}
MoveTop(n -1 , from , 1);
m_towers.MoveDisc(1,from);
MoveTop(n - 1, 1, to);
}// MoveTop
其中1是中間掛鉤。而m_tower.MoveDisc(從,)將一張光盤從一個釘到另一個釘。
這是第一次調用MoveTop函數。
void CPlayer::OnRun()
{
bool ok = MoveTop(10,0,2);
}// OnRun
我也得到一個返回上稱爲PEG板數的高度的功能,但我真的不認爲我需要它。
增加了更多的問題形容 的移動的所有圖形中的窗口,在這裏,我可以看到的板如何從栓移動到栓的結果被示出。而現在,它不會簡單地做它應該做的。在運行代碼時,第一個平臺移動到樁1(中間的樁)並「上下跳動」。
這些都是可用的功能:
高度功能:
int CTowers::Height(int tower)
{
ASSERT(torn>=0 && torn<3);
return m_height[torn];
}
具體的塔返回的高度。
int CTowers::TopSize(int tower)
{
int h = Height(tower);
if (h==0)
return 1000;
else return DiscSize(torn, h-1);
}
返回掛鉤上的大小。
int CTowers::DiscSize(int tower, int place)
{
ASSERT(place < Height(torn));
return m_pinne[tower][place];
}
返回指定的dicssize。
bool CTowers::MoveDisc(int to, int from)
{
if (m_abort)
return false; //
m_pView->DrawAnimation(from, to);
if (TopSize(from)>=TopSize(to))
return false;
m_numMoves++;
int ds = Pop(from);
Push(to, ds);
m_pView->Invalidate();
return true;
}
將光盤從一個掛鉤移動到另一個掛鉤。
請描述你的功能不起作用的方式。 – 2014-10-16 17:09:14
你一定要調用'm_towers。MoveDisc(to,1);'在遞歸函數之外,因爲在所有後續調用之前(由於函數的遞歸性質)「拆除」了這些塔。順便說一下,這個函數假設'from!= 1'和'to!= 1'。 – 2014-10-16 17:27:30
@barakmanos你能發展你的想法嗎?我應該只有'm_towers.MoveDisc(1,from);'函數內部嗎?我真的不明白你談論的問題。 – 2014-10-16 17:44:22