Very new student, Hanoi Towers
- From: Bill <grotto3b@xxxxxxxxx>
- Date: Tue, 3 Mar 2009 09:06:13 -0800 (PST)
First of all, thanks to everyone who helped me when I first asked
about this:
public class Towers {
static int nDisks = 3 ;
public static void main(String[] args) {
hanoiTower(nDisks, 'A', 'B','C');}
public static void hanoiTower( int topN, char src, char inter,
char dest)
{
if(topN == 1)
System.out.println( " Disc 1 from "+ src + " to " +
dest);
else {
hanoiTower(topN-1, src, dest, inter);
System.out.println( "Disk" + topN +"from" + src +
"to" + dest);
hanoiTower(topN-1, inter, src, dest);
}
Disc 1 from A to C
Disk2fromAtoB
Disc 1 from C to B
Disk3fromAtoC
Disc 1 from B to A
Disk2fromBtoC
Disc 1 from A to C
Please tell me where I'm not getting it. This is how I try to work it
until I fail, or it works by coincidence and I'm wrong from the
beginning::
I can see the first line of the solution being printed as a command,
not because (topN == 1) being true. And, A is src, and C is dest.,
like the arguments set.
So, on to the else, where topN becomes 2 after the hanoiTower
(topN-1,src,dest,inter) is applied. So the line "Disk2from A(still in
src slot) to B, (now in dest slot) is printed. OK.
So now on to the next hanoiTower, the last in the else, where topN
becomes 1, 'A' is now inter, 'B' is now src, 'C' is now dest..
Back up to the if(topN == 1) where it is now true. "Disc 1 from C to
B" prints, but how did src come to represent 'C', and dest come to
represent 'B'. In my logic, the last hanoiTower would read (1, inter,
dest, src) for that line to print.
Then, why does the next line, "Disk3fromAtoC" print at all when the
else should not have been entered?
I've went back to reviewing,instead of fuming, and I found another
text, albeit a much simpler one, that does not mention recursion at
all, but I would like to continue with the more intense text, by
Deitel. I've been looking for an instructor, or student instructor in
a
local college, or high school, who might be willing to give me a mini
lecture. Like other instances before, I've suddenly thought,"
Eureka!", then, "How could I have been so thick-headed for so
long?".Please help.
.
- Follow-Ups:
- Re: Very new student, Hanoi Towers
- From: Chris B
- Re: Very new student, Hanoi Towers
- Prev by Date: Re: java "gems"
- Next by Date: Re: java "gems"
- Previous by thread: java "gems"
- Next by thread: Re: Very new student, Hanoi Towers
- Index(es):
Relevant Pages
|
Loading