UVa10795 A Different Task (New Hannow tower problem)

Source: Internet
Author: User

Links: http://acm.hust.edu.cn/vjudge/problem/34699
Analysis: Consider the plate with the largest number, if the initial situation of the plate and the target situation is located on the same pillar, then there is no need to move it, because no other plate number than it, indicating that it is located in the pillar of the bottom, it does not affect the movement of other plates. In this way, in the initial situation and the target situation, we can find the most important plate in the different plates of the column, set to K, then K must move. We can imagine moving k in the moment between the pillars of the case, assuming that the plate K to move from pillar 1 to Pillar 2, because the number of plates larger than the K is not required to move, and does not interfere with, can be directly ignored. The plate with a number smaller than k can not be on the Pillar 1 (only 1 plates can be moved at a time), nor on the Pillar 2 (the number of large can not be pressed on the number of small plates), so only on the Pillar 3, in other words, at this time the column 1 is only K, Pillar 2 is empty, Pillar 3 from top to K-1 (ignoring plates larger than k), we refer to this situation as a reference. Because the plate movement is reversible, according to symmetry, we only need to ask the initial situation and the target situation to move into the reference situation of the sum of steps, and then add 1 (move plate K) can (a bit of bidirectional breadth first search flavor). To summarize, we need to write a function f (P, I, Final), which indicates that the initial column number array for each plate known as P (P[i] represents the column number of the plate I), and the number of steps required to move the plate (1,2,3,...,i) all the way to the final column. The answer to this question is F (start,k-1,6-start[k]-finish[k]) +f (Finish,k-1,6-start[k]-finish[k]) +1. Start[i] and Finish[i] is the initial column and the target pillar of plate I in the input, K is the number of the largest plate that must be moved above. We numbered the plates in 6-x-y, so that the pillars other than pillar x and column y are numbered. How to calculate f (p[i],i,final)? When P[i]=final, it is stated that the first plate is properly placed on the final pillar without moving, and then only the i-1 plate is moved to the final pillar, then F (p,i,final) =f (p,i-1,final). Otherwise, you need to move the front i-1 plate to 6-p[i]-final this pillar to do the relay, and then the first plate to move the pillar final, finally the money i-1 a plate from the column to move to the target column final, then moving the first plate of the moment, I need to move the first plate to final (1 times) and then move the front i-1 plate from the pillar of 6-p[i]-final to the final pillar (according to the classic of the Hanoi tower questionConclusion This step requires 2^ (i-1)-1) step, which adds up to a total of 2^ (i-1) steps, in other words, when p[i] is not equal to final, F (p,i,final) =f (p,i-1,6-p[i]-final) +2^ (i-1), the recursive boundary is i==0.
1#include <cstdio>2 3typedefLong LongLL;4 Const intMAXN = -+5;5 6 intN, Start[maxn], FINISH[MAXN];7 8LL F (intPintIintfinal) {9     if(i = =0)return 0;Ten     if(P[i] = = final)returnF (P, I-1, final); One     returnF (P, I-1,6-P[i]-final) + (1LL << (i-1)); A } -  - intMain () { the     intKase =0; -      while(SCANF ("%d", &n) = =1&&N) { -          for(inti =1; I <= N; i++) scanf ("%d", &start[i]); -          for(inti =1; I <= N; i++) scanf ("%d", &finish[i]); +         intK =N; -          while(k >=1&& Start[k] = = Finish[k]) k--; +LL ans =0; A         if(k >=1) { at             intother =6-Start[k]-Finish[k]; -ans = f (start, K-1, other) + F (finish, K-1, other) +1; -         } -printf"Case %d:%lld\n", ++Kase, ans); -     } -     return 0; in}

UVa10795 A Different Task (New Hannow tower problem)

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.