We consider the Towers of Hanoi problem with 7 disks of diff
We consider the \"Towers of Hanoi\" problem with 7 disks of different sizes and 3 pegs (or towers, labeled A, B, C) is described on the Wikipedia page http://en.wikipedia.org/wiki/Towers_of_Hanoi. Write a recursive MATLAB program to determine the sequence of moves to bring all disks to peg C starting with all disks at peg A, given the fact that only one disk can be moved at a time and a larger disk cannot be on top of a smaller disk. After each move record the position for each disk by the peg it is located at. The position of the pegs is recorded in the form of a 7-letter word, with letters from the set {A,B,C}. Each letter denotes the position of a disk on a given peg, from smallest to largest disk from left to right. For example ABACACA corresponds to disks 1 (smallest), 3, 5, 7(largest) on tower A, disk 2 on tower B, and disks 4, 6 on tower C. The initial and final positions are recorded as AAAAAAA and CCCCCCC, respectively. Determine the sequence of moves, each line of your ouput containing a 7 letters words corresponding to the position of the disks.
Solution
We can write the recursive code for tower of hanoi
Base Case: When (n=1)
then we have to just the disk from peg to topeg
else
ew move (n-1) disks from A to B, then move the last disk from A to C and then recursively move the remaining (n-1) disk from B to C
Matlab Code:
function[] = towersofhanoi(n,A,B,C)
if(n==1)
fprintf(\'\\t move disk from peg %c to peg %c \ \', A,C);
else
towers(n-1,A,C,B);
fprintf(\'\\t move disk (n-1) from peg %c to peg %c \ \', A,C);
towers(n-1,C,B,A);
end;
List of moves
