# 16.1 Tower of Hanoi Variant 2

#1

Hello everyone, I have doubts on how to solve the variant 2 of tower of hanoi, I modified the code for the variant that roughly works(don’t know exactly). Can anyone show how it’s actually done?

``````void towerOfHanoicounter(int n, char from_rod, char to_rod, char aux_rod)
{
if (n == 1)
{
t++;
if(from_rod != 'c' and to_rod != 'c'){
printf("\n Move disk 1 from rod %c to rod %c", from_rod, aux_rod);
t++;
printf("\n Move disk 1 from rod %c to rod %c", aux_rod, to_rod);
}else{
printf("\n Move disk 1 from rod %c to rod %c", from_rod, to_rod);
}
return;
}
if(from_rod != 'c' and to_rod != 'c') {
towerOfHanoicounter(n - 1, from_rod, aux_rod, to_rod);
towerOfHanoicounter(n - 1, aux_rod, to_rod, from_rod);
printf("\n Move disk %d from rod %c to rod %c", n, from_rod, aux_rod);
towerOfHanoicounter(n - 1, to_rod, aux_rod, from_rod);
towerOfHanoicounter(n - 1, aux_rod, from_rod, to_rod);
printf("\n Move disk %d from rod %c to rod %c", n, aux_rod, to_rod);
t+=2;
towerOfHanoicounter(n - 1, from_rod, to_rod, aux_rod);
}else{
towerOfHanoicounter(n-1, from_rod, aux_rod, to_rod);
printf("\n Move disk %d from rod %c to rod %c", n, from_rod, to_rod);
t++;
towerOfHanoicounter(n-1, aux_rod, to_rod, from_rod);
}
}
``````

0 Likes

#2

@vsr625 You can take a look at my solution here. Tests here.

Note: I implemented recursion with a stack, but besides that the algorithm shouldn’t be all that different from other correct ones, assuming mine is correct - which I believe it is. Unlike the “main” problems, correctness of the variant solutions isn’t easy to confirm as most people don’t publish their solutions, if they actually solve them at all.

1 Like

#3

Thanks! I was trying to do the same thing with my code(Although my coding isn’t that good).
and since I was trying to only solve for the restriction of P3, I didn’t generalize it well.
I will be checking out your repository, thanks!
I don’t know how to run kotlin, can you output the answer for the restriction of P3, with number of rings = 3, 4

0 Likes

#4

Hi @vsr625, Here is my solution and answers for 3 and 4.

`````` public static void towersOfHanoi2(int count, String from, String to, String temp) {
if(count > 0) {
towersOfHanoi2(count-1, from, to, temp);
System.out.println("Move ring '" + count + "' from '" + from + "' to '" + temp + "'");
towersOfHanoi2(count-1, to, from, temp);
System.out.println("Move ring '" + count + "' from '" + temp + "' to '" + to + "'");
towersOfHanoi2(count-1, from, to, temp);
}
}
``````

Solution for 3 rings:
Move ring ‘1’ from ‘A’ to ‘C’
Move ring ‘1’ from ‘C’ to ‘B’
Move ring ‘2’ from ‘A’ to ‘C’
Move ring ‘1’ from ‘B’ to ‘C’
Move ring ‘1’ from ‘C’ to ‘A’
Move ring ‘2’ from ‘C’ to ‘B’
Move ring ‘1’ from ‘A’ to ‘C’
Move ring ‘1’ from ‘C’ to ‘B’
Move ring ‘3’ from ‘A’ to ‘C’
Move ring ‘1’ from ‘B’ to ‘C’
Move ring ‘1’ from ‘C’ to ‘A’
Move ring ‘2’ from ‘B’ to ‘C’
Move ring ‘1’ from ‘A’ to ‘C’
Move ring ‘1’ from ‘C’ to ‘B’
Move ring ‘2’ from ‘C’ to ‘A’
Move ring ‘1’ from ‘B’ to ‘C’
Move ring ‘1’ from ‘C’ to ‘A’
Move ring ‘3’ from ‘C’ to ‘B’
Move ring ‘1’ from ‘A’ to ‘C’
Move ring ‘1’ from ‘C’ to ‘B’
Move ring ‘2’ from ‘A’ to ‘C’
Move ring ‘1’ from ‘B’ to ‘C’
Move ring ‘1’ from ‘C’ to ‘A’
Move ring ‘2’ from ‘C’ to ‘B’
Move ring ‘1’ from ‘A’ to ‘C’
Move ring ‘1’ from ‘C’ to ‘B’

0 Likes