C + + realizes steel strip cutting problem

Source: Internet
Author: User

Looking at the dynamic programming (DP) in the introduction to the algorithm today, there is an interesting question: the steel strip cutting to get the maximum benefit.

Several solutions are discussed, including recursive solution method, Memo DP solution, bottom-up solution and the reconstruction of the solution.
The book gives the pseudo code of different solutions, just need to practice C + +, there is C + + to achieve DP solution to the steel bar cutting problem.

"Recursive solver"

Steel bar cutting problem//top-down recursive implementation # include <iostream> #include <time.h>using namespace std;int cut_rod (int len, int price_a    Rr[]); int main (int argc, char *argv[]) {clock_t start, finish;    Double duration;    start = Clock ();    int rod_len = 10;    int p_arr[] = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30};    int profit = Cut_rod (Rod_len, P_arr);    cout << "Profit =" << profit << Endl;    finish = Clock ();  Duration = (double) (Finish-start)/clocks_per_sec;    Units s cout << "time Cost =" << duration * + << "MS" << Endl; return 0;} /* Recursive call to Cut_rod function * Input: Rod length len, Unit bar length Price table price_arr[] * Output: Len's best yield for steel bars * * thought: * 1. If len==0, return 0 * 2. Otherwise, the steel bar is cut to the left length of I and the right length of len-i two segments, * length of I is not in the cut, the profit is Price_arr[i], the right part continues to decompose. * Note: * 1. There is an access risk to the program, and when the input length is greater than p_arr[] length, the elements outside the array are accessed; * Therefore the program is valid only for the data of Len < sizeof (P_arr)/sizeof (int); */int cut_rod (int len, int p    Rice_arr[]) {if (len = = 0) return 0;    int best_profit =-100; for (iNT i = 1; I <= Len;    i++) {best_profit = max (Best_profit, Price_arr[i] + cut_rod (len-i, Price_arr)); } return best_profit;}

"Bottom-up DP reconstruction Solution"

The bottom-up reconstruction solution includes the bottom-up solution, so that only the reconstruction method is transmitted here.

The article assumes that the price is still $30 when the bar length is greater than 10 inches, so there is no limit to the length of the input bar < of course it is unscientific >;

/* DP solves the problem of strip cutting * Not only returns the maximum yield of the steel bar with len length r[], and returns the cutting condition s[] (the solution of the refactoring) * The solution of the reconstruction is also determined by the nature of the sub-problem. */#include <iostream> #include < string.h> #define Rod_len 17#define len Rod_len + 1using namespace std;void extend_bottom_up_cut_rod (int len, int price_  arr[], int (&r) [Len], int (&s) [Len]), void print_cut_rod_solution (int LEN, int price_arr[], int (&r) [Len], int (&s) [LEN]);    int main (int argc, char *argv[]) {int price[] = {0, 1, 5, 8, 9, 10, 17, 17, 20, 24, 30};    int R[len], S[len];    memset (r, 0, sizeof (r));    memset (s, 0, sizeof (s));    Print_cut_rod_solution (Rod_len, Price, R, s); return 0;} void Extend_bottom_up_cut_rod (int len, int price_arr[], int (&r) [Len], int (&s) [Len]) {if (len = = 0) ret    Urn        for (int j = 1; j <= Len; j + +) {int p =-1000;            for (int i = 1; I <= J; i++)//J-Length steel strip, cut i {int left_price;            if (i >) left_price = price_arr[10]; else Left_price = Price_arr[i];                if (P < Left_price + r[j-i]) {p = left_price + r[j-i];            S[J] = i;    }} R[j] = p; }}void print_cut_rod_solution (int len, int price_arr[], int (&r) [Len], int (&s) [Len]) {Extend_bottom_up_cut_rod    (Len, Price_arr, R, s);    cout << len << "inch rod Price is" << R[len] << Endl;    cout << "The Cut Order is";        while (Len > 0) {cout << S[len] << "";    Len-= S[len]; }}

  

C + + realizes steel strip cutting problem

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.