Using recursive algorithm to realize N-layer Hanoi animation demonstration matlab code and explanation

Source: Internet
Author: User

Hanoi (Hanoi), also known as Hanoi, is a puzzle toy that originated from an ancient Indian legend. When big Brahma created the world, he made three diamond pillars, and stacked 64 gold discs on a pillar from bottom to top in order of size. The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time.

First, take the three-storey Hanoi as an example to illustrate:

Three pillars are numbered a,b,c, the discs from small to large numbered respectively for the three, then the implementation of the use of pillar B to achieve the disc from a to C process as follows:

Number of discs move the number of disk numbers move step

1 1 1 a->c

Number of discs move the number of disk numbers move step

2 1 1 a->b

2 2 2 A->c

2 3 1 B->c

Number of discs move the number of disk numbers move step

    3                              1                       1                          a->c

    3                              2                       2                         A- >b     3                              3                       1                          c->b 

3 4 3 A->c

3 5 1 B->a

    3                              6                       2                          B->C     3                              7                       1                          a->c 
Fully available: 1, when the disc number and the number of disks, both perform a->c operation;

2, the implementation of 2 discs from the a->b,a->c,b->c and other operations are the same;

Inference: When there are n disks, it can be decomposed into n-1,n-2,n-3 ... 2 discs are executed to form recursion, and n-1 pillars are executed each time with the help of C to achieve a to B, then a to C, and finally the implementation of the B-C process with a.

When there are only 1 discs, the MATLAB code is implemented as follows:

function hanoi1t
Global A B C m
Global Move_times Pause_times
Pause_times=1;
move_times=0;
CLC
Subplot (1,3,1);
Line ([-1 1],[1 1], ' linewidth ', 30);
Axis ([ -1.5,1.5,0,3]);
Axis off;
Line ([0 0],[1 3], ' linewidth ', ten, ' Color ', ' black ');
Subplot (1,3,2);
Line ([0 0],[1 3], ' linewidth ', ten, ' Color ', ' black ');
Axis ([ -1.5,1.5,0,3]);
Axis off;
Subplot (1,3,3);
Line ([0 0],[1 3], ' linewidth ', ten, ' Color ', ' black ');
Axis ([ -1.5,1.5,0,3]);
Axis off;
Pause (pause_times);
ClF
move_times=move_times+1;
fprintf (' moved%d times\n ', move_times);
Subplot (1,3,3);
Line ([-1 1],[1 1], ' linewidth ', 30);
Axis ([ -1.5,1.5,0,3]);
Axis off;
Line ([0 0],[1 3], ' linewidth ', ten, ' Color ', ' black ');
Subplot (1,3,2);
Line ([0 0],[1 3], ' linewidth ', ten, ' Color ', ' black ');
Axis ([ -1.5,1.5,0,3]);
Axis off;
Subplot (1,3,1);
Line ([0 0],[1 3], ' linewidth ', ten, ' Color ', ' black ');
Axis ([ -1.5,1.5,0,3]);
Axis off;
Operation Result:








When the number of disks is N:

function Hannuota (N,TA1,TA2,TA3)
% Hanoi
If N==1
fprintf ('%c-->%c\n ', ta1,ta3);
Else
Hannuota (N-1,TA1,TA3,TA2);
fprintf ('%c-->%c\n ', ta1,ta3);
Hannuota (N-1,TA2,TA1,TA3);
End
End



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.