Tower of Hanoi Problem

조회 수: 4 (최근 30일)
SB
SB 2012년 11월 9일
편집: Jerome Bastien 2021년 10월 20일
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
Kelly Tatiana Acosta Contreras 2021년 6월 28일
%
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에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by