Tower of Hanoi Problem

32 views (last 30 days)
SB
SB on 9 Nov 2012
Edited: Jerome Bastien on 20 Oct 2021
So, I tried to implement code that solves the Tower of Hanoi Problem (which I had previously used in python), and it sort of worked, but outputted
Move disk 1 from tower 65 to tower 67
Move disk 2 from tower 65 to tower 67
Move disk 1 from tower 66 to tower 65
for towers_of_hanoi(2,'A','B','C') (the problem being that it outputted 65 instead of A, 66 for B and 67 for C which I'm guessing is ASCII)
How would I avoid this issue while keeping similar code? Or do i need to use something like vertcat? Please help. My code is below:
%
function towers_of_hanoi(n,A,B,C)
if (n~=0)
towers_of_hanoi(n-1,A,B,C);
disp(sprintf('Move disk %d from tower %d to tower %d',[n A C]));
towers_of_hanoi(n-1,B,C,A);
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 9 Nov 2012
I already gave the solution in your previous posting on this topic, which you appear to have deleted.
Use %s instead of %d when you want to output strings.
  2 Comments
SB
SB on 9 Nov 2012
I actually tried that( I forgot to update the code), but when I do
%
function towers_of_hanoi(n,A,B,C)
if (n~=0)
towers_of_hanoi(n-1,A,B,C);
disp(sprintf('Move disk %d from tower %s to tower %s',[n A C]));
towers_of_hanoi(n-1,B,C,A);
end
end
It outputs:
Move disk 1 from tower AC to tower
Move disk 2 from tower AC to tower
Move disk 1 from tower BA to tower
Instead of the proper solution.
Walter Roberson
Walter Roberson on 9 Nov 2012
disp(sprintf('Move disk %d from tower %s to tower %s',n, A, C))
Alternately,
fprintf('Move disk %d from tower %s to tower %s',n, A, C);
which is the same thing except with the display of the string built-in.

Sign in to comment.

More Answers (4)

Chenyang Huang
Chenyang Huang on 26 Jan 2016
Edited: Chenyang Huang on 26 Jan 2016
function towers_of_hanoi(n,A,C,B)
if (n~=0)
towers_of_hanoi(n-1,A,B,C);
disp(sprintf('Move disk %d from tower %c to tower %c',[n A C]));
towers_of_hanoi(n-1,B,C,A);
end
end
------
>> towers_of_hanoi(3,'A','C','B')
  1 Comment
Walter Roberson
Walter Roberson on 26 Jan 2016
There is no apparent question or comment there?
I do not recommend this code; it relies upon using [] to concatenate a number and two characters, and then relies upon MATLAB pulling them apart again. There is no good reason to use [n A C] there instead of n, A, C as separate arguments. And you might as well use %s . In fact you might as well use the fprintf() that I showed:
fprintf('Move disk %d from tower %s to tower %s',n, A, C);

Sign in to comment.


Sarvesh Agarwal
Sarvesh Agarwal on 9 Feb 2018
How to find the no. of moves by a particular disc. Let's just say for total 10 disks what are the no. of moves by 4th disk provided topmost is 1st disk. Thanks in advance.
  1 Comment
Walter Roberson
Walter Roberson on 9 Feb 2018
With 10 disks, the largest disk (#10) moves 2^0 times, the next largest (#9) moves 2^1 times, the next largest (#8) moves 2^2 times, and so on.

Sign in to comment.


Kelly Tatiana Acosta Contreras
%
function towers_of_hanoi(n,A,B,C)
if (n~=0)
towers_of_hanoi(n-1,A,B,C);
disp(sprintf('Move disk %d from tower %d to tower %d',[n A C]));
towers_of_hanoi(n-1,B,C,A);
end
end

Jerome Bastien
Jerome Bastien on 20 Oct 2021
Edited: Jerome Bastien on 20 Oct 2021
Caution, there is a small mistake in the answears of Chenyang Huang : the correct line is
disp(sprintf('Move disk %d from tower %d to tower %d',[n A C]));
and no
disp(sprintf('Move disk %d from tower %c to tower %c',[n A C]));
The final correct code is :
function towers_of_hanoi(n,A,C,B)
if (n~=0)
towers_of_hanoi(n-1,A,B,C);
% Erreur
% disp(sprintf('Move disk %d from tower %c to tower %c',[n A C]));
disp(sprintf('Move disk %d from tower %d to tower %d',[n A C]));
towers_of_hanoi(n-1,B,C,A);
end
end

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!