Packing Rectangles chapter1.4

开始完全没有思路,不知如何贪心下手,后来看到这篇博主的结题报告,研究了会才搞懂

http://www.cnblogs.com/kissfinger/archive/2011/03/08/1977720.html

然后自己仿照着写,到situation5(就是第6幅图,4,5图一种情况)时还是出错了,后来想想我少考虑了一种情况

自己的是max2(a.width+b.width,c.width+d.width),应该是max3(a.width+b.width,c.width+d.width,b.width+d.width)

  1 /*
  2 
  3 ID: hubiao cave
  4 
  5 PROG: packrec
  6 
  7 LANG: C++
  8 
  9 */
 10 
 11 
 12 
 13 
 14 #include<iostream>
 15 #include<fstream>
 16 #include<string>
 17 #include<algorithm>
 18 #include<set>
 19 using namespace std;
 20 
 21 //#define max2(a,b) (a>b?a:b)
 22 //#define max3(a,b,c) (a>max2(b,c)?a:max2(b,c))
 23 //#define max4(a,b,c,d) (a>max3(b,c,d)?a:max3(b,c,d))
 24 
 25 int max2(int a,int b)
 26 {
 27     return a>b?a:b;
 28 }
 29 int max3(int a,int b, int c)
 30 {
 31     return max2(max2(a,b),c);
 32 }
 33 int max4(int a,int b,int c,int d)
 34 {
 35     return max2(max2(a,b),max2(c,d));
 36 }
 37 
 38 
 39 
 40 struct rect
 41 {
 42     int width;
 43     int height;
 44 };
 45 rect rc[4];
 46 int index[4]={0,1,2,3};
 47 
 48 void swap(rect&);
 49 
 50 void work(rect,rect,rect,rect);
 51 
 52 void situ1(rect,rect,rect,rect);
 53 void situ2(rect,rect,rect,rect);
 54 void situ3(rect,rect,rect,rect);
 55 void situ4(rect,rect,rect,rect);
 56 void situ5(rect,rect,rect,rect);
 57 
 58 bool operator<(const rect& rc1,const rect&rc2)
 59 {
 60     return rc1.width<rc2.width;
 61 }
 62 
 63 void update(int,int);
 64 
 65 int miniarea=999999;
 66 std::set<rect> sr;
 67 
 68 int main()
 69 
 70 {
 71 
 72     ifstream fin("packrec.in");
 73     ofstream fout("packrec.out");
 74     for(int i=0;i<4;i++)
 75     {
 76         fin>>rc[i].height>>rc[i].width;
 77     }
 78 
 79     for(int i=0;i<2;i++)
 80     {
 81         swap(rc[0]);
 82         for(int j=0;j<2;j++)
 83         {
 84             swap(rc[1]);
 85             for(int m=0;m<2;m++)
 86             {
 87                 swap(rc[2]);
 88                 for(int n=0;n<2;n++)
 89                 {
 90                     swap(rc[3]);
 91                     
 92                     do 
 93                     {
 94                         work(rc[index[0]],rc[index[1]],rc[index[2]],rc[index[3]]);
 95 
 96                     } while (std::next_permutation(index,index+4));
 97                 }
 98             }
 99         }
100     }
101 
102     fout<<miniarea<<endl;
103     for(set<rect>::iterator it=sr.begin();it!=sr.end();it++)
104     {
105         fout<<(*it).width<<" "<<(*it).height<<endl;
106     }
107     return 0;
108 
109 
110 }
111 
112 void swap(rect&rc1)
113 {
114     int n=rc1.height;
115     rc1.height=rc1.width;
116     rc1.width=n;
117 }
118 
119 void situ1(rect a,rect b,rect c,rect d)
120 {
121     int maxh=max4(a.height,b.height,c.height,d.height);
122     int totalw=a.width+b.width+c.width+d.width;
123     update(maxh,totalw);
124 }
125 
126 void situ2(rect a,rect b,rect c,rect d)
127 {
128     int maxw=max2(d.width,a.width+b.width+c.width);
129     int maxh=max3(a.height,b.height,c.height)+d.height;
130     update(maxh,maxw);
131 }
132 
133 void situ3(rect a,rect b,rect c,rect d)
134 {
135     int maxw=c.width+max2(a.width+b.width,d.width);
136     int maxh=max2(max2(a.height,b.height)+d.height,c.height);
137     update(maxh,maxw);
138 }
139 
140 void situ4(rect a,rect b,rect c,rect d)
141 {
142     int maxw=a.width+c.width+max2(b.width,d.width);
143     int maxh=max3(a.height,c.height,b.height+d.height);
144     update(maxh,maxw);
145 }
146 
147 void situ5(rect a,rect b,rect c,rect d)
148 {
149     int maxh=max2(a.height+d.height,b.height+c.height);
150     int maxw;
151     if(c.height>=a.height+d.height)
152     {
153         maxw=max2(max2(a.width,d.width)+c.width,b.width);
154     }
155     else
156     {
157         if(c.height>=d.height)
158         {
159             maxw=max3(a.width+b.width,c.width+d.width,a.width+c.width);
160         }
161         if(c.height<d.height&&c.height+b.height>d.height)
162         {
163             maxw=max3(a.width+b.width,c.width+d.width,b.width+d.width);
164         }
165         if(b.height+c.height<=d.height)
166         {
167             maxw=max2(a.width,max2(b.width,c.width)+d.width);
168         }
169 
170 
171     }
172     update(maxh,maxw);
173 }
174 
175 
176 void update(int wid,int height)
177 {
178     rect rc1;
179     int area=wid*height;
180 
181     if(wid>height)
182     {
183         int temp=height;
184         height=wid;
185         wid=temp;
186     }
187     rc1.height=height;
188     rc1.width=wid;
189 
190     if(area<miniarea)
191     {
192         sr.clear();
193         sr.insert(rc1);
194         miniarea=area;
195         return;
196     }
197     if(area==miniarea)
198     {
199         sr.insert(rc1);
200         return;
201     }
202     return;
203     
204 
205 }
206 
207 void work(rect a,rect b,rect c,rect d)
208 {
209     situ1(a,b,c,d);
210     situ2(a,b,c,d);
211     situ3(a,b,c,d);
212     situ4(a,b,c,d);
213     situ5(a,b,c,d);
214 
215 }
原文地址:https://www.cnblogs.com/cavehubiao/p/3250016.html