Hdoj HDU 2064 tower iii acm 2064 in HDU

Source: Internet
Author: User
// Original miyu, please note: Reprinted from __________ White House

Question address:
Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 2064
Description:
Tower III
About the end of the 19th century, I sold an intellectual toy 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?

Tower of Hanoi is a classic recursive instance,If the rule is not so abnormal, allow direct jump from 1 to 3, then n disks need at least 2n-1.

Some new rules are added here. We can analyze them as follows:NDisk (s) from1Move3:

Step 1: initial status:


Step 1: Move n-1-1 disks above to the 2nd ROD:

Step 1: Move the nth disk from 1 to 2:

Step 1: Move the first n-1 from 3 to 1 to give way to the first disk:

Step 1: Move the nth disk from 2 to 3:

Step 1: Move the first n-1 from 3 to complete the movement:

We set F (n) to the number of steps required to move n disks from 1 to 3, which is also the number of steps required to move from 3 to 1.

From the figure above, we can see that it takes three steps to move the nth disk from 1 to 3:

1) Move the first n-1 from 1. 3.

2) the nth disk must go through two steps from 1-> 2-> 3.

3.) n-1-1 Disk needs to be 3-> 1 (this is to give way to the n-disk), and then 1-> 3.

When F (n) = 3 × F (n-1) + 2;

F (1) = 2;

In this way, we get the recursive formula for this question. Of course, we can further optimize it. The optimization method is as follows:

F (n) = 3 × F (n-1) + 2
F (1) = 2
=>
F (n) + 1 = 3 × [F (n-1) + 1]
F (1) + 1 = 2 + 1 = 3
=>
F (n) + 1 = 3n
=>
F (n) = 3N-1
Last pastedCode:
 // Original miyu, please note: Reprinted from __________ White House

# Include < Iostream >
# Include < Cmath >
Using   Namespace STD;
Long   Long MYPOW ( Int N, Int E)
{
Long   Long MLT =   1 ;
For ( Int I =   1 ; I <= E; ++ I)
{
MLT * = N;
}
Return MlT;
}
Int Main ()
{
Int N;
While (CIN > N)
{
Cout < MYPOW ( 3 , N) -   1   < Endl;
}
Return   0 ;
}

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.