![]() MATLAB / Octave coding style and readability.I would appreciate your opinion and suggestions related to: Input: Move 4 disks from 1st to 3rd peg, 2nd is free. % M: 1-by-K matrix where M(i) is the max power of 2 such % Returns m: max power of 2 such that 2^m divides n, and + 1 % Add one and get moves of disk in Tower of Hanoi. = Omega2(N) % Generate the first N terms of: "Binary Carry Sequence". % in Tower of Hanoi where the index is the move number % Returns a 1xN integer array with the first N consecutive disk moves ![]() % Accepts integer: N - total number of moves. % so: mod(j - 1, len) + 1, to avoid index = 0.įrom = path1( mod(j - 1, len) + 1) % Cycle over 1->2->3ĬurrentPositions(i) = j % update moves of i-th diskĭisp(sprintf('Move disk %d from %d to %d.', i, from, to)) J = currentPositions(i) % j - number of moves for i-th disk If mod(i, 2) = 0 % if number of disk, i is even % If N (numer of disks) is even the paths are swapped. Path2 = % Path of disk with even number.ĬurrentPositions = ones(1, N) % index-disk number, value-number of moves Path1 = % Path of disk with odd number: from->alt->to % These are the paths of disks if N is odd. M = generateDiskMoves(totalNumberOfMoves) % in the second argument to one with number in third arg. % the Towers of Hanoi with N disks moved from tower with number stored % Returs string outputs with succesive moves to complete the task of solving % from - number of start tower, to - number of end tower, alt - free tower. % Accepts three integers: N - number of disks Here is an implementation of Towers of Hanoi based on few observed patterns 1 from the easier recursive solution: function = myTowersOfHanoi(N, from, to, alt)
0 Comments
Leave a Reply. |