Article View: be.comp.programming
Article #13996Re: Towers of Hanoi
From: Wim Dumon
Date: Mon, 07 May 2001 08:24
Date: Mon, 07 May 2001 08:24
28 lines
1707 bytes
1707 bytes
Rory Fitzpatrick wrote:
>
> Does anyone Know an algorithm to the towers of Hanoi problem using c++ with
> recursion
That's not so hard:
void hanoi(int nr_discs, int source, int destination, int helper)
{
if (nr_discs == 1)
cout << "move disc from " << source << " to " << destination << endl;
else {
hanoi(nr_discs - 1, source, helper, destination);
cout << "move disc from " << source << " to " << destination << endl;
hanoi(nr_discs - 1, helper, destination, source);
}
}
void do_hanoi(int nr_discs)
{
hanoi(nr_discs, 1, 3, 2);
}
This will give you a 10 on your homework assignment.
Greetings,
Wim.
Technical Details
Message-ID:
<3AF65BC1.57112FC3@easics.be>Path:
arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-02!supernews.com!bignews.mediaways.net!unlisys!news.snafu.de!news.tele.dk!134.222.94.5!npeer.kpnqwest.net!nreader2.kpnqwest.net.POSTED!not-for-mailReferences:
<dOQI6.30557$PF4.55868@news.iol.ie>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fcea4,3c949c5ad80563b8 X-Google-Attributes: gidfcea4,public X-Google-ArrivalTime: 2001-05-07 01:27:35 PST Path: arc1-ca.pugleaf.net!newsfeed.google.com!sn-xit-02!supernews.com!bignews.mediaways.net!unlisys!news.snafu.de!news.tele.dk!134.222.94.5!npeer.kpnqwest.net!nreader2.kpnqwest.net.POSTED!not-for-mail Message-ID: <3AF65BC1.57112FC3@easics.be> From: Wim Dumon <wim@easics.be> Organization: Easics nv, Leuven, Belgium, http://www.easics.com X-Mailer: Mozilla 4.72 [en] (X11; U; Linux 2.2.17-14 i686) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: be.comp.programming Subject: Re: Towers of Hanoi References: <dOQI6.30557$PF4.55868@news.iol.ie> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 28 Date: Mon, 07 May 2001 08:24:33 GMT NNTP-Posting-Host: 193.74.198.7 X-Complaints-To: abuse@Belgium.EU.net X-Trace: nreader2.kpnqwest.net 989223873 193.74.198.7 (Mon, 07 May 2001 10:24:33 MET DST) NNTP-Posting-Date: Mon, 07 May 2001 10:24:33 MET DST