Ex 6_14 布料剪裁问题_第八次作业

子问题定义: 定义p[i][j]为布料宽为i,高为j的最优产出,每次剪下一块布料,剩余布料最多形成三块矩阵面料。每次剪裁会有两种情况,水平切割布料,其次是将布料旋转90度后在切割布料。

递归关系:

初值设定:

       p[0][h]=0

       p[w][0]=0

求解顺序:

       依次求解二维数组p的每一行,每一列,最终的结果为p[布料的宽][布料的高]

 1 package org.xiu68.ch6.ex8;
 2 
 3 public class Ex6_14 {
 4 
 5     //布料剪裁问题
 6     public static void main(String[] args) {
 7         // TODO Auto-generated method stub
 8         Clothing[] cloths=new Clothing[]{
 9                 new Clothing(10,10,10),
10                 new Clothing(20,20,10),
11                 new Clothing(30,30,30),
12                 new Clothing(40,40,160)
13         };    
14         tailor(10,10,cloths); //生产出的产品最高价为: 10
15         tailor(40,40,cloths); //生产出的产品最高价为: 160
16 
17         Clothing[] cloths2=new Clothing[]{
18                 new Clothing(10,10,10),
19                 new Clothing(10,10,11),
20                 new Clothing(20,20,10),
21                 new Clothing(30,30,30),
22                 new Clothing(40,40,180)
23         };    
24         tailor(10,10,cloths2); //生产出的产品最高价为: 11
25         tailor(40,40,cloths2); //生产出的产品最高价为: 180
26     }
27 
28     /*
29      * w:布料的宽
30      * h:布料的高
31      * cloths:服装产品
32      * 一块布切割后,最多剩下3块
33      */
34     public static void tailor(int w,int h,Clothing[] cloths){
35         if(w<=0 || h<=0)
36             return;
37         int[][] p=new int[w+1][h+1];            //p[i][j]表示布料宽为i,高为j所得到的最大价格
38         
39         //求布料的宽为i,高为j所得到的最大价格
40         for(int i=1;i<=w;i++){
41             for(int j=1;j<=h;j++){
42                 p[i][j]=0;
43                 for(int k=0;k<cloths.length;k++){
44                     
45                     int kWidth=cloths[k].width,        //第k件服装的需要布料的宽
46                         kHeight=cloths[k].height,    //第k件服装的需要布料的高
47                         kPrice=cloths[k].price,        //第k件服装的价格
48                         horizontalCut=0,            //水平切割布料所得到的最大价格
49                         verticalCut=0;                //旋转布料90度后再切割布料所得到的最大价格
50                     
51                     if(i>=cloths[k].width && j>=cloths[k].height)    //水平切割
52                         horizontalCut=kPrice+p[kWidth][j-kHeight]+p[i-kWidth][kHeight]+p[i-kWidth][j-kHeight];
53                     
54                     if(i>=cloths[k].height && j>cloths[k].width)    //旋转布料90度后再切割
55                         verticalCut=kPrice+p[kHeight][j-kWidth]+p[i-kHeight][kWidth]+p[i-kHeight][j-kWidth];
56                     
57                     if(horizontalCut>verticalCut){
58                         if(horizontalCut>p[i][j])
59                             p[i][j]=horizontalCut;                        
60                     }else{
61                         if(verticalCut>p[i][j])
62                             p[i][j]=verticalCut;
63                     }
64                     
65                 }//for3
66             }//for2
67         }//for1
68         System.out.println("生产出的产品最高价为: "+p[w][h]);
69     }
70 }
71 
72 //服装
73 class Clothing{
74     public int width;        //
75     public int height;        //
76     public int price;        //价格
77     
78     public Clothing(int width, int height, int price) {
79         super();
80         this.width = width;
81         this.height = height;
82         this.price = price;
83     }
84 }
View Code
原文地址:https://www.cnblogs.com/xiu68/p/7988910.html