하노이의 탑 구하는 과정좀 부탁드립니다.
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
문제해결 부탁드립니다.
0 Kommentare
Antworten (2)
Riya
am 19 Nov. 2023
Hello,
제 모국어는 한국어가 아니라서, 이 질문에 영어로 답변하려고 합니다. 이해해주셔서 감사합니다.
As per my understanding, you want to know the algorithm for Tower of Hanoi.
Please note that Tower of Hanoi consists of three rods and a number of disks of different sizes, which can slide onto any rod. The objective is to move the entire stack of disks from one rod to another, following these rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack or an empty rod.
3. No disk may be placed on top of a smaller disk.
Here's an example implementation of the Tower of Hanoi in MATLAB using recursion:
function TowerOfHanoi(n, source, auxiliary, destination)
if n > 0
% Move n-1 disks from source to auxiliary rod
TowerOfHanoi(n-1, source, destination, auxiliary);
% Move the nth disk from source to destination rod
fprintf('Move disk %d from rod %c to rod %c\n', n, source, destination);
% Move the n-1 disks from auxiliary to destination rod
TowerOfHanoi(n-1, auxiliary, source, destination);
end
end
To use this function, you can call it with the number of disks and the labels for the rods:
n = 3; % Number of disks
TowerOfHanoi(n, 'A', 'B', 'C');
In this example, the disks are initially on rod 'A', and we want to move them to rod 'C', using rod 'B' as the auxiliary rod. You can modify the number of disks and the rod labels as per your requirements.
When you run this code, it will output the sequence of moves required to solve the Tower of Hanoi puzzle for the given number of disks.
I hope it helps.
Regards,
Riya Arora
0 Kommentare
Angelo Yeo
am 26 Dez. 2023
MATLAB Answers is a place you can get help from the user community on specific MATLAB questions. It is not intended as a place to have strangers do your homework for you. Chances are your professor would not appreciate you turning in other people's code as your own work. Not only is it lazy, but it is cheating.
Desiring help with homework is fine, but:
- Show what you have done so far (post some code and explain what you are thinking about how to solve the problem).
- Ask a specific question about either specific MATLAB syntax/constructs, or general guidance (such as "what functions should I look at to achieve [result]")
- If your code needs some data or a file at run time, then attach your data file with the paperclip icon. Otherwise we cannot run your code. Some people may go to the trouble to create some sample data, but don't depend on that -- attach your own data.
- Make sure your question is answerable. A non-answerable question looks like, "How can I use MATLAB for Image Processing."
0 Kommentare
Siehe auch
Kategorien
Mehr zu Financial Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!