Effacer les filtres
Effacer les filtres

Tower of Hanoi Problem

16 vues (au cours des 30 derniers jours)
SB
SB le 9 Nov 2012
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

Réponse acceptée

Walter Roberson
Walter Roberson le 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 commentaires
SB
SB le 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 le 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.

Connectez-vous pour commenter.

Plus de réponses (4)

Chenyang Huang
Chenyang Huang le 26 Jan 2016
Modifié(e) : Chenyang Huang le 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 commentaire
Walter Roberson
Walter Roberson le 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);

Connectez-vous pour commenter.


Sarvesh Agarwal
Sarvesh Agarwal le 9 Fév 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 commentaire
Walter Roberson
Walter Roberson le 9 Fév 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.

Connectez-vous pour commenter.


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 le 20 Oct 2021
Modifié(e) : Jerome Bastien le 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

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by