Tower of Hanoi algorithm demonstration 1.0

Source: Internet
Author: User

I was bored after work, so I came up with the idea of using js to implement algorithm demonstration. I used js to implement selection and sorting a long time ago, but the source program could not be found!

The Recursive Algorithm of qingta:

Void move (int n, char a, char B, char c) {If (n = 1) printf ("\ t % C-> % C \ n",, c); // when N has only one, it moves directly from A to C else {move (n-1, A, C, B ); // The n-1 part is to be moved from A to B printf through C ("\ t % C-> % C \ n", a, c); move (n-1, B,, c); // change B to start disk after N-1 moves, and move B to C through a, which is hard to understand }}

In the above recursive algorithm, the printf function can be replaced by our JS animation processing function. I was inspired by the piata algorithm demo written by others: the animation effect was first executed after the algorithm was executed.

[Demo address]

JS address of the tower of Hanoi algorithm: [hnt. js]

 

In the init () method, we can see that

// Initialize hnt. init = function (OPT) {$ ("# block" ).html (''); hnt. movie = []; hnt. options = $. extend (hnt. options, OPT); hnt. a. num = hnt. options. n; hnt. b. num = 0; hnt. c. num = 0; hnt. initblock (hnt. options. n); hnt. move (hnt. options. n, "A", "B", "C"); hnt. start ();}

The first half is to use the variable for reinitialization.

Second half:

Initblock (); // initialize the plate

Move (); // The execution of the pie chart algorithm is only an execution algorithm. In this process, the path of the plate movement is saved, but no animation is executed.

Start (); // execute the animation effect.

 

========================================================== ====================

Later: Now, I just used js to simply implement the demonstration process of the tower of Hanoi. Next I plan to optimize the process and the interface, and I hope I can learn something else. For example, HTML5 DND, audio, woker, localstorage, and jquerymobile frameworks

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.