Hanoi Tower Problem (recursive, stack)

Source: Internet
Author: User

Modify the rules of the game Hanoi, now can not directly from the left to the right, nor directly to the right to the left.

Method One: Recursive implementation

Now, for example, if there is a 1~n on the left, then the last case of movement is:

1.1-n-1 move from left to right

2.N move from left to middle

3.1-n-1 move from right to left

4.N move from Middle to right

5.1-n-1 move from left to right

So, if I have such an F (range,from,to) then I need to solve is f (n,lrft,right), the atomic situation is from only a few times, the right side of the center directly to the left.

1   Public Static intProcessintnum, string from, string to) {2         if(num = = 1) {3System.out.println (' Move 1 from ' + from + ' to Middle ');4System.out.println ("Move 1 from Middle to" +to );5             return2;6         }7         Else {8             intP1 = Process (num-1, from, to);9System.out.println (' move ' + num + ' from ' + from + ' to Middle ');Ten             intP2 = Process (num-1, to, from); OneSystem.out.println ("move" + num + "from Middle to" +to ); A             intP3 = Process (num-1, from, to); -             returnP1 + p2 + p3 +2; -         } the}
View Code

Method two: Using stacks to implement

Thinking like this, there are altogether four movements: l2m m2l m2r r2m

At any one time, to the minimum number of moves, and to meet the principle of small pressure, only one situation can be satisfied.

The proof is as follows:

If the last action is l2m, then it is impossible to l2m (small pressure Large), it is impossible to m2l (optimal), then the remaining m2r and r2m, there must be a situation will be met. Other things in the same vein.

So use three stacks to record the left and right of the number, and then each time to detect the one that satisfies the condition to continue. Since each stack is stripped of the top of the stack element to compare, so in order to avoid judging whether it is empty, the top of each stack is first pressed into the maximum value to the most secure. The end condition is that the number of elements in the rightmost stack is the total number of elements.

1   Public Static enumAction {2 no,l2m, m2l,m2r,r2m3     }4      Public Static intHANOIPROBLEM2 (intnum) {5stack<integer> ls =NewStack<integer>();6Stack<integer> ms =NewStack<integer>();7Stack<integer> rs =NewStack<integer>();8 Ls.push (integer.max_value);9 Ms.push (integer.max_value);Ten Rs.push (integer.max_value); One          for(inti = num; i > 0; i--) { A Ls.push (i); -         } -action[] Preaction ={action.no}; the         intStep = 0; -          while(Rs.size ()! = num + 1) { -Step + =Fstack2stack (preaction,action.m2l,action.l2m,ls,ms); -Step + =Fstack2stack (preaction,action.l2m,action.m2l,ms,ls); +Step + =Fstack2stack (preaction,action.r2m,action.m2r,ms,rs); -Step + =Fstack2stack (preaction,action.m2r,action.r2m,rs,ms); +         } A         returnStep; at     } -      Public Static intfstack2stack (action[] preaction, action noaction, action Curaction, -Stack<integer> Fstack, stack<integer>tstack) { -         if(Preaction[0] = = Noaction | | fstack.peek () >=Tstack.peek ()) { -             return0; -         } inPreaction[0] =curaction; - Tstack.push (Fstack.pop ()); to         return1; +}
View Code

Hanoi Tower Problem (recursive, stack)

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.