Tower of Hanoi Problem

16 次查看(过去 30 天)
SB
SB 2012-11-9
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

采纳的回答

Walter Roberson
Walter Roberson 2012-11-9
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 个评论
SB
SB 2012-11-9
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 2012-11-9
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.

请先登录,再进行评论。

更多回答(4 个)

Chenyang Huang
Chenyang Huang 2016-1-26
编辑:Chenyang Huang 2016-1-26
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 个评论
Walter Roberson
Walter Roberson 2016-1-26
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);

请先登录,再进行评论。


Sarvesh Agarwal
Sarvesh Agarwal 2018-2-9
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 个评论
Walter Roberson
Walter Roberson 2018-2-9
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.

请先登录,再进行评论。


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 2021-10-20
编辑:Jerome Bastien 2021-10-20
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

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by