"Hanoi tower Problem" UVa 10795-a Different Task

Source: Internet
Author: User

"Classic Hanoi tower Problem"

Hanoi (Hanoi) Tower problem: There is a Vatican tower in ancient times, there are three seats in the tower A, B, C,a seats on 64 plates, the size of the plate, large in the lower, small on the. There is a monk who wants to move the 64 plates from block A to block B, but only one plate can be moved at a time, and in the course of movement, the plates on the 3 seats remain on the market and the small plate is on. In the move process can take advantage of block B, require printing the steps to move. If there is only one plate, you do not need to use block B to move the plate directly from a to C.

    • If there are 2 plates, you can first move plate 1 on plate 2 to B; Move plate 1 to C; Move plate 2 to C. This shows: You can use B to move 2 plates from A to C, of course, you can also use C to move 2 plates from A to B.
    • If there are 3 plates, then according to the conclusion of the 2 plates, C can be used to move the two plates on plate 1 from A to B, the plate 1 from a to c,a into empty seats, and the two plates on the B to C by means of block A. This means that 3 plates can be moved from one seat to another with an empty seat.
    • If you have 4 plates, first move the three plates on plate 1 from A to B with the help of the empty seat C, move the plate 1 to the c,a, and move the three plates on block B to C with the empty block A.

namely: F (N) =f (n-1) +1+f (n-1) = (2^n)-1; F (1) = 1;

The code is as follows:

1#include <iostream>2#include <cstdio>3 using namespacestd;4 5 intStep =0;6 voidMove (CharSour,Chardest)7 {8printf ("move from%c to%c \ n", Sour, dest);9 }Ten voidHanoi (intNCharSour,CharTempChardest) One { A     if(n = =1) -     { - Move (sour, dest); the++Step; -     } -     Else -     { +Hanoi (n1, sour, dest, temp); - Move (sour,dest); +++Step; AHanoi (n1, temp, sour, dest); at     } - } - intMain () - { -     intn =4; -Hanoi (N,'A','B','C' ); inprintf ("Total Steps is%d\n", step); -     return 0; to}
View Code

"The new question of the Nottingham Tower"

Given the initial situation and the target situation, it takes at least a few steps from the initial situation to the target situation.

Analysis:

    • Reference situation: To move the plate K on its initial column and K on no other plate, K of the target pillar is empty, the middle pillar from top to bottom is 1,2,...,k-1 plate.
    • First, the largest plate K is not found on the target pillar, because if the largest plate is on the target pillar it does not need to move, nor is it in the way.
    • The rest of the task is similar to the classic Hanoi Tower, which is about to move the k-1 plate to the reference, move the plate K to the target pillar, and then move the k-1 plate to the target (as the movement is reversible, it can be seen as a target plate moving to a reference situation). It can be seen that this is a recursive problem.

That is, we need a function f (P, I, Final): The number of steps required to move all plates numbered 1~i to the final column (the array P represents the initial column number of each plate, except the initial column x of the plate and the final pillar y the remaining pillar is numbered 6-x-y, because only the pillar 1, 2, 3);

Then: ans = f (start,k-1,6-start[k]-finish[k]) +f (finish,k-1,6-start[k]-finish[k]) +1;

As for the calculation of the function f, when I is 0 o'clock, there is no need to move; otherwise the 1~i-1 plate is moved to the reference situation. If the i-1 plate itself is on the relay pillar, it is equivalent to moving only the 1~i-2 plate to the final pillar. Move 1~i-1 Plate from the reference situation to the goal of the process is the classic Hanoi tower process, the number of steps is 2^ (i-1)-1; Here the answer is to use a long long;

The code is as follows:

1#include <iostream>2#include <cstdio>3#include <cstdlib>4 using namespacestd;5 Const intMAXN = $;6 intN, A[maxn], B[MAXN];7 //F (P, I, Final): The number of steps required to move all plates numbered 1~i to the final column8 //(the array p represents the initial column number of each plate, minus the initial pillar of the plate X and the final pillar y the remaining pillar is numbered 6-x-y)9 Long LongFintPintIintFin)Ten { One     if(!i)return 0; A     if(P[i] = = Fin)returnF (P, I-1, Fin); -     returnF (P, I-1,6-p[i]-fin) +1+ (1LL << (i-1)) -1); - } the intMain () - { -     intKase =0; -      while(SCANF ("%d", &n) &&N) +     { -          for(inti =1; I <= N; i++) +         { Ascanf"%d", &a[i]); at         } -          for(inti =1; I <= N; i++) -         { -scanf"%d", &b[i]); -         } -         intK =N; in          while(k && A[k] = = B[k]) k--; -  to         Long LongAns =0; +         if(k >=1) -         { the             intTMP =6-a[k]-B[k]; *ans = f (A, K1, TMP) +1+ F (B, K1, TMP); $         }Panax Notoginsengprintf"Case %d:%lld\n", ++Kase, ans); -     } the     return 0; +}

Hanoi Tower problem UVa 10795-a Different Task

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.