HDU2064 tower III [recurrence], hdu2064 tower iii

Source: Internet
Author: User

HDU2064 tower III [recurrence], hdu2064 tower iii

Tower IIITime Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission (s): 9235 Accepted Submission (s): 4125

Problem Description about the end of the 19th century, an intellectual toy was sold in a store in ozhou with three poles on a copper plate, on the leftmost bar, the Tower consists of 64 disks in ascending order. The purpose is to move all the disks on the leftmost bar to the right bar, with the condition that only one disk can be moved at a time and the tray cannot be placed on a small disk.
Now we can change the gameplay. We cannot directly move from the leftmost (rightmost) side to the rightmost (leftmost) side (each move must be moved to or from the middle ), you cannot place the dashboard on the lower disk.
Daisy has already done the original tower problem and Tower II, but when she encountered this problem, she thought for a long time and couldn't solve it. Please help her now. Now there are N discs. How many times does she move these discs from the leftmost to the rightmost?
 
Input contains multiple groups of data. Each Input has an N value (1 <= N = 35 ).
For each group of data, Output moves the smallest number of times.
Sample Input
1312
 
Sample Output
226531440
Assume that step f [n] is required to move the n-layer tower from A to C by B. The specific moving process can be seen as follows: It takes step f [n-1] To Move layer n from A to C through B, and then move layer n from A to B. One step is required, next, we need to move the n-1 layer from C to A by B. Step f [n-1] is required. Then we need to move the n-th layer from B to C. One step is required, next, we need to move the n-1 layer from A to C through B. Step f [n-1] is required, in total 3 * f [n-1] + 2, where f [1] = 2;

#include <stdio.h>__int64 dp[36] = {0, 2};int main(){    int n;    for(n = 2; n < 36; ++n) dp[n] = dp[n-1] * 3 + 2;    while(scanf("%d", &n) == 1) printf("%I64d\n", dp[n]);    return 0;}



How to deduce the tower's Formula

The following recursive formula is obtained when the N plates of hannotta need to be moved several times:

A [1] = 1;
A [n] = a [n-1] * 2 + 1;

Ask the general term formula?

A [1] = 1;
A [n] = a [n-1] * 2 + 1;

A [I] = 2 ^ I-1;

It proves that mathematical induction is adopted:
1. Conjecture a [I] = 2 ^ I-1
2. When I = 1, it is clearly true.
3. Assume that I = k is true, that is, a [k] = 2 ^ k-1; then:
Obtained from a [n] = a [n-1] * 2-1;
A [k + 1] = a [k] * 2-1
= 2 ^ k * 2-1
= 2 ^ (k-1)-1
Therefore, you must pass the verification.

At the same time ::
The Hanoi Tower issue (also known as the Hanoi Tower issue) is a problem based on legends:

There are three poles A, B, and C. There are N (N> 1) perforated disks on rod A, and the size of the disks decreases from bottom to top. Move all disks to the C rod according to the following rules:

1. Only one disc can be moved at a time;
2. The dashboard cannot be stacked on a small disk.

Tip: You can temporarily place the disc on rod B, or move the disc removed from Rod A back to Rod A, but both of them must follow the above two rules.

Q: How to migrate data? How many times does it need to be moved at least?
Generally, N = 64 is used. In this way, you need to move the limit for at least one time. That is, if a disc can be moved in one second, it will take 584.554 billion years. According to the theory of the Big Bang, the age of the universe is only 13.7 billion years.

In real toys, generally N = 8; this will need to be moved 255 times. If N = 10, you need to move it 1023 times. If N = 15, it needs to be moved 32767 times; that is to say, if a person moves a disc from 3 to 99 years old, he can only move 15 pieces a day. If N = 20, it needs to be moved 1048575 times, that is, more than 1 million times.
First look at hanoi (1, one, two, three. In this case, a plate on the one column is directly moved to the three column. Note: it is not important whether one or three is A, B, or C, remember that a disk on the column represented by the second parameter of the function is moved to the column represented by the fourth parameter. For convenience, this action is recorded:
One = "three

Let's look at hanoi (2, one, two, three. Considering that hanoi (1) has been analyzed, three actions are actually generated:
One = "two
One = "three
Two =, three
Obviously, this is actually equivalent to moving the two disks on the one column directly to the three column.

Let's look at hanoi (3, one, two, three. Analysis
Hanoi (2, one, three, two)
One = "three
Hanoi (2, two, one, three)
That is: first move the two disks on the one column to the two column, then move one disk on the one column to the three column, and then move the two disks on the two column to the three column. Does this mean that the three disks on the one column are directly moved to the three column?

Using Induction, we can see that for any n,
Hanoi (n-1, one, three, two)
One = "three
Hanoi (n-1, two, one, three)
It is to first move the n-1 disks on one column to the two column, then one disk on one column to the three column, and then the n-1 disks on the two column to the three column. This is what we need!
Respondent: wuchenghua121-manager level 4 12-5
Tower of Hanoi
The Hanoi Tower issue is an ancient legend of India. Brama, the God of heaven and earth, left three diamond sticks in a temple. The first one was built above ...... the rest of the article>

Tower of Hanoi Problem Solving

I think this tower is the most difficult example in Tan haoqiang's book. It took me a long time to understand. I wrote some of my summary in the book to you. I don't know if you can clear
Void move (char x, char y)
{
Printf ("% c --> % c \ n", x, y );
}
I. Void hanoi (int n, char one, char two, char three)
{
If (n = 1) move (one, three );
Else {
II. Hanoi (n-1, one, three, two );
3. Move (one, three );
4. Hanoi (n-1, two, one, three );
}
}
Main ()
{
Int m;
Printf ("Number of input towers ");
Scanf ("% d", & m );
Hanoi (m, 'A', 'B', 'C ');
}
I have marked four areas above
Assume that 3 is entered here.
The role of 1 is to move from 1 seat to 2 seat
The role of 2 is to move n-1 plates to 2. Do not worry about how to move them because recursive operations are required.
3. Move the Bottom Plate of the prefix to C.
4 is to move the remaining N-1 plate on the 2 seat to C
The above 2, 4 are recursive.
The running process is as follows:
Input 3
Void hanio (int 3, char x, char y, char z)
Hanoi (n-1, one, three, two );
* **************** → (2-1, x, y, z) → (1, x, y, z) → x → z
* *** (3-1, x, z, y) → run 2 ********************** x → y
* **************** → (2-1, z, x, y) → (1, z, x, y) * z → y
Move (one, three );
Run 3 ************************************** x → z
Hanoi (n-1, two, one, three );
Run 4
* ***************** → (2-1, y, z, x) → (1, y, z, x) → y-X
(3-1, y, x, z) *
(2-1, x, y, z) → (1, x, y, z) * ***************** x → z
I wonder if you can see it clearly.
The answer is:
X-> z;
X-> y;
Z → y,
X-> z;
Y-> x;
Y-> z;
X-> z;

Related Article

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.