Hanoi Tower´s problem using recursion
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
João Domingos
am 28 Jul. 2015
Kommentiert: Walter Roberson
am 29 Jul. 2015
Hello i´m developping a program to solve the hanoi tower problem using a recoursive function but it doesn´t seens to work.I´m going to put the code that i have writen and going to explain the ideia and the data matrixes.
Main program:
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
N=5;
torre_final =1;
vec_movimentos = zeros ((power(2,N)-1),4);
for i=1:size(vec_movimentos,1)
vec_movimentos(i,1)=i;
end
tabuleiro = zeros(N,3);
tabuleiro(:,1)=1:N;
n_jogada =1;
[vec_movimentos] = hanoi_code(N,torre_final,vec_movimentos,n_jogada,tabuleiro);
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
hanoi_code is my recursive function and the matrix 'tabuleiro' represents the 3 tower´s with N disk. 'vec_movimentos' is a matrix that describes the moves that the user has to do in order to resolve the problem. The first column of 'vec_movimentos' is the counter of the number of moves; the second column is what piece the user has to move; the third column is the inicial tower that the piece is located at; the forth column is the final cloumn that the piece is going to be move to;'torre_final' is a variable with 2 possible values: 1 or 2. If torre_final = 1 then the solution is going to be formed in the center tower; if torre_final=2 then the solution is going to exist in the final right tower
hanoi_code:
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function [vec_movimentos] =hanoi_code(N,torre_final,vec_movimentos,n_jogada,tabuleiro)
%torre_final == 1 consiste no deslocamento para a torre 1 posição à direita
%torre_final == 2 consiste no deslocamento para a torre 2 posições à
%direita
if N > 0;
hanoi_code(N-1,torre_final,vec_movimentos,n_jogada,tabuleiro);
shift(N,torre_final,vec_movimentos,n_jogada,tabuleiro);
hanoi_code(N-1,torre_final,vec_movimentos,n_jogada,tabuleiro);
else
return;
end
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
we have here the recursive code ,'n_jogada' indicates what the current move is.
shift:
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
function [vec_movimentos] =shift(N,torre_final,vec_movimentos,n_jogada,tabuleiro)
vec_movimentos(n_jogada,2)=N;
[inicio,fim]= game(N,tabuleiro,torre_final);
vec_movimentos(n_jogada,3)=inicio;
vec_movimentos(n_jogada,4)=fim;
n_jogada =n_jogada+1;
end
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
It´s not necessary to display the game code because the program doesn´t get there and i have already test this part of the code and it seens to work.
My main problem is that the recursion seens not to work. When i run the program the 'vec_movimentos' remains the same , and the 'tabuleiro' also remains in the original configuration. The 'n_jogadas' that should = 31 , is equal =1. 31 because the numbers of moves if 2^(N)-1 , and N=5. Any ideia what´s the problem? What´s wrong with the recursion part?
Thank you in advance
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 29 Jul. 2015
Remember that calling
hanoi_code(N-1,torre_final,vec_movimentos,n_jogada,tabuleiro);
computes a value by calling hanoi_code, and then discards the value because you did not assign it to a variable and the semi-colon says not to bother to print the result either.
3 Kommentare
Stephen23
am 29 Jul. 2015
Bearbeitet: Stephen23
am 29 Jul. 2015
vec_movimentos = hanoi_code(N-1,torre_final,vec_movimentos,n_jogada,tabuleiro);
vec_movimentos = shift(N,torre_final,vec_movimentos,n_jogada,tabuleiro);
MATLAB uses pass-by-value, not pass-by-reference, so if you want the variable to change then you need to allocate it as an output.
Weitere Antworten (1)
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!