Nyoj89 tower (2), nyoj89 Tower

Source: Internet
Author: User

Nyoj89 tower (2), nyoj89 Tower

Question Web site: http://acm.nyist.net/JudgeOnline/problem.php? Pid = 89

Classic conclusion of the tower of Hanoi Problem:

Moving a plate from one pillar to another requires at least one step to the power of 2. In this case, we should give him the minimum number of steps required to reach the target (all move to the third pillar. What should I do !! Analysis:

1. In general, we must first move the largest plate to the third pillar, and then move the second largest plate to Pillar 3, then move the third-largest plate to column 3 ......... it is not finished until the smallest plate (plate 1) is moved to column 3.

2. Now imagine the overall situation on the pillar before moving the k-th plate. Assume that the k-th plate is on column 1, and move it to column 3, since all the dishes larger than k have been moved, you do not need to consider it. At this time, all the dishes smaller than k should be on column 2, because they cannot be on column 1 or column 3, at this time, the plates on column 2 are numbered 1, 2, 3 ....... k-1.

3. Find the largest plate and start to move it from the largest one. If the largest plate is already on pillar 3 (the target column), you do not need to move it. So we should find the largest plate not on pillar 3.

4. Here we will first talk about the Function ac (I, x), which indicates the minimum number of steps required to move all the previous I plates to the ground x columns. The k plates (on column 1) For example: Move the k plate to column 3 before the moment on the column is: 1 to K-1 plate on column 2, k is on 1. Ac (K-1, 2) is the number of steps required to move to one State. At this time, it takes one step to move k to column 3. If you want to move all the dishes to 3, you also need to move 2 to 1 ~ K-1 plates move all over Pillar 3. It is also known that the classic tower of the conclusions of the mobile K-1 plate needs 2 K-1 power minus one. Then it is concluded that the total number of required steps is: ac (K-1, 2) + 1 + pow (2, k-1)-1 = ac (K-1, 2) + pow (2, k-1 );

 

# Include <iostream> # include <cstdio> # include <string. h> # include <math. h> using namespace std; long ans, a [10000] [5]; int t, n, mx, star [10000]; long ac (int x, int t) {if (x = 1) {if (star [x] = t) return 0; else return 1;} if (a [x] [t]! =-1) return a [x] [t]; if (star [x] = t) // If the x plate has been on the target column, it will not move, directly consider moving the next a [x] [t] = ac (x-1, t ); else a [x] [t] = ac (x-1, 6-t-star [x]) + pow (2, x-1); // total of three plate numbers 6, it cannot be on the target column t or on the column x to be moved. It can only return a [x] [t] On 6-t-satr [x];} int main () {cin> t; while (t --) {memset (a,-1, sizeof (a); scanf ("% d", & n); mx = 0; for (int I = 1; I <= n; I ++) {scanf ("% d", & star [I]); if (I> mx & star [I]! = 3) mx = I;} if (mx = 0) printf ("0 \ n"); else if (mx = 1) printf ("1 \ n"); else if (mx> 1) {ans = ac (mx-1, 3-star [mx]); ans + = pow (2, mx-1); printf ("% ld \ n", ans) ;}} return 0 ;}View Code

 

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.