Poj 1958 strange towers of Hanoi (DP)

Source: Internet
Author: User
Http://poj.org/problem? Id = 1958

The first question given to the algorithm.

The process is very clear. First, move n-k to B using four columns, and then move K to D using three columns, finally, move n-k to D using four columns. N-k are moved twice in total. We know the minimum number of steps for moving the three columns is 2 ^ n-1, the total number of moving steps is f [I-j] * 2 + T [J]. The equation f [I] = min (F [I], f [I-j] * 2 + T [J]).

Because we are more confident, we can directly use the large-sized materials, without using Majia. It would be dumbfounded once we handed in the paper, and re! Well, I really deserve it. Why is the array so small? Don't ask for money...

Code:

# Include <iostream>
# Define min (A, B) A> B? B:
Using namespace STD;

Int T [13], F [13];
Int main (){
Int I, J;
For (I = 0; I <= 12; I ++)
T [I] = (1 <I)-1;
For (I = 1; I <= 12; I ++ ){
F [I] = 100;
For (j = 1; j <= I; j ++)
F [I] = min (F [I], F [I-j] * 2 + T [J]);
Cout <F [I] <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.