JS Analog Stack---Hanoi

Source: Internet
Author: User

var Stack = (function () {var items = new Weakmap (),//first-in, first-out class Stack{constructor () {items.set (this,[]);} Push (Ele) {//in stack var ls = items.get (this); var len = ls.length;if (len > 0 && ls[len-1] <= ele) {throw new Error ( "Hanrotta error") return; Ls.push (ele);} Pop () {//out of stack var ls = items.get (this); return Ls.pop ();} Size () {//Gets the length of the stack var ls = items.get (this); return ls.length;} Print () {///stack var ls = items.get (this); return ls.tostring ();}} return Stack;})  var stack1 = new Stack (), var stack2 =new stack (), var stack3 = new stack (); const num = 3; Hanoi series for (var i = num; I >= 1; i--) {Stack1.push (i);}        var obj = {"A": Stack1, "B": Stack2, "C": Stack3}function Hanoi (Disc,a,b,c) {if (disc>0) {Hanoi (DISC-1,A,C,B);        Console.log (' move ' + ' + ' + ' + ' + ' disc ' + ' move from ' + A + ' to ' + C '); Obj[c].push (Obj[a].pop ());    Transfer array Hanoi (DISC-1,B,A,C); }}//Before starting Console.log (Stack1.print ()); Console.log (Stack2.print ()); Console.log (Stack3.print ()); Console.log ("------------------------------"); Hanoi (obj["a"].size (), "a", "B", "C");//Results Console.log (Stack1.print ()); Console.log (Stack2.print ()); Console.log ( Stack3.print ());

  

"Hanoi" is an ancient Indian legend, but also the classic recursion problem in program design, is a famous puzzle game:

The topics are as follows:

There are three pillars on the tower and a set of hollow discs of different diameters, and all the discs on the source pillars are arranged in order from large to small in the beginning. The goal is to move a disk to another pillar each time, eventually moving a pile of discs to the target column, the process is not allowed to place the larger disk on the smaller disk;

Find the pattern (move all the discs to C):

1) n (number of discs) = = 1

First time: Number 1th, A-C sum (number of moves) = 1

2) n = = 2

First: Plate 1th, A-B

Second time: Plate 2nd, A-C

Third: Number 1th, B-C sum = 3

3) n = = 3

First: Plate 1th, A-C

Second time: Plate 2nd, A-B

Third: Plate 1th, C-B

Fourth time: Plate 3rd, C

Fifth time: Plate 1th, B, A

Sixth time: Plate 2nd, B, C

Seventh time: Number 1th, A-C sum = 7

etc...

So it is not difficult to find the law, the number of moves: sum = 2^n-1

Algorithm Analysis (Recursive):

Move a pile of discs from one pillar to another, using auxiliary pillars if necessary. You can divide it into three sub-problems:

First, move the smaller discs in a pair of discs to the auxiliary pillars, exposing the larger discs below,

Next, move the disc below to the target column.

Finally, move the smaller disc from the auxiliary pillar to the target post.

Turn three steps into a simple math problem:

(1) Move the n-1 plate from a to B;

(2) Move the nth plate from a to C;

(3) Move the n-1 plate from B to C;

For example, there are 4 plates on a.
(1) Move 1-3 from A to B
(2) Move 4 from A to C
(3) Move 1-3 from B to C

We create a JS function that, when it calls itself, handles the disk that is currently working on the disk. Finally it goes back to a non-existent disk to call, in which case it is not performing any operations.

JS Analog Stack---Hanoi

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.