Borrar filtros
Borrar filtros

Tower of Hanoi Problem

16 visualizaciones (últimos 30 días)
SB
SB el 9 de Nov. de 2012
Editada: Jerome Bastien el 20 de Oct. de 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

Respuesta aceptada

Walter Roberson
Walter Roberson el 9 de Nov. de 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 comentarios
SB
SB el 9 de Nov. de 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 el 9 de Nov. de 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.

Iniciar sesión para comentar.

Más respuestas (4)

Chenyang Huang
Chenyang Huang el 26 de En. de 2016
Editada: Chenyang Huang el 26 de En. de 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 comentario
Walter Roberson
Walter Roberson el 26 de En. de 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);

Iniciar sesión para comentar.


Sarvesh Agarwal
Sarvesh Agarwal el 9 de Feb. de 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 comentario
Walter Roberson
Walter Roberson el 9 de Feb. de 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.

Iniciar sesión para comentar.


Kelly Tatiana Acosta Contreras
Kelly Tatiana Acosta Contreras el 28 de Jun. de 2021
%
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 el 20 de Oct. de 2021
Editada: Jerome Bastien el 20 de Oct. de 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

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by