POJ1022 Packing Unit 4D Cubes

题目来源:http://poj.org/problem?id=1022

题目大意:

  有一些4维的单位体积的立方体盒子,每个立方体有8个面。要用一个大的4为盒子将它们包起来,求最小的大盒子体积。

输入:第一行为测试用例数。每个用例的第一行为单位立方体数目n。接下来的n行每行为一个立方体的信息。每行第一个数字为还立方体的编号,接下来的8个整数分别为对应面相邻的立方体的编号。该面没有邻居则为0.(给出的都是单一刚体。)

输出:最小的能把这个由小4D立方体拼起来的形状的盒子的体积。


Sample Input

1
9
1 2 3 4 5 6 7 8 9
2 0 1 0 0 0 0 0 0
3 1 0 0 0 0 0 0 0
4 0 0 0 1 0 0 0 0
5 0 0 1 0 0 0 0 0
6 0 0 0 0 0 1 0 0
7 0 0 0 0 1 0 0 0
8 0 0 0 0 0 0 0 1
9 0 0 0 0 0 0 1 0

Sample Output

81

本题题干描述得很复杂,想象起来也有一些抽象,其实很简单,跟3D的情况联系起来想就可以了。3D求包围盒的方法推广至4D即可。

  1 //
  2 //        POJ1022 Packing Unit 4D Cubes
  3 //        Memory: 300K        Time: 16MS
  4 //        Language: C++        Result: Accepted
  5 //
  6 
  7 #include <iostream>
  8 #include <vector>
  9 #include <map>
 10 
 11 using namespace std;
 12 
 13 class Cube {
 14 public:
 15     int x1p, x1n, x2p, x2n, x3p, x3n, x4p, x4n;
 16 };
 17 class Pos {
 18 public:
 19     int id;
 20     int x1, x2, x3, x4;
 21 };
 22 
 23 int main() {
 24     int ncase;
 25     cin >> ncase;
 26     for (int caseNo = 1; caseNo <= ncase; ++caseNo) {
 27         int n;
 28         map<int, Cube> cubes;
 29         cin >> n;
 30         for (int i = 1; i <= n; ++i) {
 31             int id;
 32             cin >> id;
 33             Cube cube;
 34             cin >> cube.x1p >> cube.x1n >> cube.x2p >> cube.x2n
 35                 >> cube.x3p >> cube.x3n >> cube.x4p >> cube.x4n;
 36             cubes[id] = cube;
 37         }
 38         bool ok = true;
 39         vector<Pos> solid;
 40         Pos firstPos;
 41         firstPos.id = (*cubes.begin()).first;
 42         firstPos.x1 = firstPos.x2 = firstPos.x3 = firstPos.x4 = 0;
 43         solid.push_back(firstPos);
 44         for (map<int, Cube>::iterator itc = cubes.begin(); itc != cubes.end(); ++itc) {
 45             Cube cube1;
 46             int id = (*itc).first;
 47             int x1p = (*itc).second.x1p;
 48             //x1p
 49             if (x1p != 0) {
 50                 if (cubes[x1p].x1n != id) {
 51                     ok = false;
 52                     break;
 53                 }
 54                 bool f = true;
 55                 Pos pos;
 56                 for (vector<Pos>::iterator its = solid.begin(); its != solid.end(); ++its) {
 57                     if (f == false) break;
 58                     if ((*its).id == id) {
 59                         pos.id = x1p;
 60                         pos.x1 = (*its).x1 + 1;
 61                         pos.x2 = (*its).x2;
 62                         pos.x3 = (*its).x3;
 63                         pos.x4 = (*its).x4;
 64                         for (vector<Pos>::iterator itr = solid.begin(); itr != solid.end(); ++itr) {
 65                             if ((*itr).id == x1p) {
 66                                 f = false;
 67                                 break;
 68                             }
 69                         }
 70                     }
 71                 }
 72                 if (f == true) { 
 73                     solid.push_back(pos);
 74                 }
 75             }
 76 
 77             //x1n
 78             int x1n = (*itc).second.x1n;
 79             if (x1n != 0) {
 80                 if (cubes[x1n].x1p != id) {
 81                     ok = false;
 82                     break;
 83                 }
 84                 bool f = true;
 85                 Pos pos;
 86                 for (vector<Pos>::iterator its = solid.begin(); its != solid.end(); ++its) {
 87                     if (f == false) break;
 88                     if ((*its).id == id) {
 89                         pos.id = x1n;
 90                         pos.x1 = (*its).x1 - 1;
 91                         pos.x2 = (*its).x2;
 92                         pos.x3 = (*its).x3;
 93                         pos.x4 = (*its).x4;
 94                         for (vector<Pos>::iterator itr = solid.begin(); itr != solid.end(); ++itr) {
 95                             if ((*itr).id == x1n) {
 96                                 f = false;
 97                                 break;
 98                             }
 99                         }
100                     }
101                 }
102                 if (f == true) { 
103                     solid.push_back(pos);
104                 }
105             }
106 
107             //x2p
108             int x2p = (*itc).second.x2p;
109             if (x2p != 0) {
110                 if (cubes[x2p].x2n != id) {
111                     ok = false;
112                     break;
113                 }
114                 bool f = true;
115                 Pos pos;
116                 for (vector<Pos>::iterator its = solid.begin(); its != solid.end(); ++its) {
117                     if (f == false) break;
118                     if ((*its).id == id) {
119                         pos.id = x2p;
120                         pos.x1 = (*its).x1;
121                         pos.x2 = (*its).x2 + 1;
122                         pos.x3 = (*its).x3;
123                         pos.x4 = (*its).x4;
124                         for (vector<Pos>::iterator itr = solid.begin(); itr != solid.end(); ++itr) {
125                             if ((*itr).id == x2p) {
126                                 f = false;
127                                 break;
128                             }
129                         }
130                     }
131                 }
132                 if (f == true) { 
133                     solid.push_back(pos);
134                 }
135             }
136             //x2n
137             int x2n = (*itc).second.x2n;
138             if (x2n != 0) {
139                 if (cubes[x2n].x2p != id) {
140                     ok = false;
141                     break;
142                 }
143                 bool f = true;
144                 Pos pos;
145                 for (vector<Pos>::iterator its = solid.begin(); its != solid.end(); ++its) {
146                     if (f == false) break;
147                     if ((*its).id == id) {
148                         pos.id = x2n;
149                         pos.x1 = (*its).x1;
150                         pos.x2 = (*its).x2 - 1;
151                         pos.x3 = (*its).x3;
152                         pos.x4 = (*its).x4;
153                         for (vector<Pos>::iterator itr = solid.begin(); itr != solid.end(); ++itr) {
154                             if ((*itr).id == x2n) {
155                                 f = false;
156                                 break;
157                             }
158                         }
159                     }
160                 }
161                 if (f == true) { 
162                     solid.push_back(pos);
163                 }
164             }
165 
166             //x3p
167             int x3p = (*itc).second.x3p;
168             if (x3p != 0) {
169                 if (cubes[x3p].x3n != id) {
170                     ok = false;
171                     break;
172                 }
173                 bool f = true;
174                 Pos pos;
175                 for (vector<Pos>::iterator its = solid.begin(); its != solid.end(); ++its) {
176                     if (f == false) break;
177                     if ((*its).id == id) {
178                         pos.id = x3p;
179                         pos.x1 = (*its).x1;
180                         pos.x2 = (*its).x2;
181                         pos.x3 = (*its).x3 + 1;
182                         pos.x4 = (*its).x4;
183                         for (vector<Pos>::iterator itr = solid.begin(); itr != solid.end(); ++itr) {
184                             if ((*itr).id == x3p) {
185                                 f = false;
186                                 break;
187                             }
188                         }
189                     }
190                 }
191                 if (f == true) { 
192                     solid.push_back(pos);
193                 }
194             }
195             //x3n
196             int x3n = (*itc).second.x3n;;
197             if (x3n != 0) {
198                 if (cubes[x3n].x3p != id) {
199                     ok = false;
200                     break;
201                 }
202                 bool f = true;
203                 Pos pos;
204                 for (vector<Pos>::iterator its = solid.begin(); its != solid.end(); ++its) {
205                     if (f == false) break;
206                     if ((*its).id == id) {
207                         pos.id = x3n;
208                         pos.x1 = (*its).x1;
209                         pos.x2 = (*its).x2;
210                         pos.x3 = (*its).x3 - 1;
211                         pos.x4 = (*its).x4;
212                         for (vector<Pos>::iterator itr = solid.begin(); itr != solid.end(); ++itr) {
213                             if ((*itr).id == x3n) {
214                                 f = false;
215                                 break;
216                             }
217                         }
218                     }
219                 }
220                 if (f == true) { 
221                     solid.push_back(pos);
222                 }
223             }
224             //x4p
225             int x4p = (*itc).second.x4p;
226             if (x4p != 0) {
227                 if (cubes[x4p].x4n != id) {
228                     ok = false;
229                     break;
230                 }
231                 bool f = true;
232                 Pos pos;
233                 for (vector<Pos>::iterator its = solid.begin(); its != solid.end(); ++its) {
234                     if (f == false) break;
235                     if ((*its).id == id) {
236                         pos.id = x4p;
237                         pos.x1 = (*its).x1;
238                         pos.x2 = (*its).x2;
239                         pos.x3 = (*its).x3;
240                         pos.x4 = (*its).x4 + 1;
241                         for (vector<Pos>::iterator itr = solid.begin(); itr != solid.end(); ++itr) {
242                             if ((*itr).id == x4p) {
243                                 f = false;
244                                 break;
245                             }
246                         }
247                     }
248                 }
249                 if (f == true) { 
250                     solid.push_back(pos);
251                 }
252             }
253             //x4n
254             int x4n = (*itc).second.x4n;
255             if (x4n != 0) {
256                 if (cubes[x4n].x4p != id) {
257                     ok = false;
258                     break;
259                 }
260                 bool f = true;
261                 Pos pos;
262                 for (vector<Pos>::iterator its = solid.begin(); its != solid.end(); ++its) {
263                     if (f == false) break;
264                     if ((*its).id == id) {
265                         pos.id = x4n;
266                         pos.x1 = (*its).x1;
267                         pos.x2 = (*its).x2;
268                         pos.x3 = (*its).x3;
269                         pos.x4 = (*its).x4 - 1;
270                         for (vector<Pos>::iterator itr = solid.begin(); itr != solid.end(); ++itr) {
271                             if ((*itr).id == x4n) {
272                                 f = false;
273                                 break;
274                             }
275                         }
276                     }
277                 }
278                 if (f == true) { 
279                     solid.push_back(pos);
280                 }
281             }
282         }
283         if (solid.size() != n) {
284             ok = false;
285         }
286         if (ok == false) {
287             cout << "Inconsistent" << endl;
288             continue;
289         }
290         int x1min = 1000;
291         int x1max = -1000;
292         int x2min = 1000;
293         int x2max = -1000;
294         int x3min = 1000;
295         int x3max = -1000;
296         int x4min = 1000;
297         int x4max = -1000;
298         for (vector<Pos>::iterator it = solid.begin(); it != solid.end(); ++it) {
299             if (x1min >(*it).x1) x1min  = (*it).x1;
300             if (x1max < (*it).x1) x1max = (*it).x1;
301             if (x2min >(*it).x2) x2min  = (*it).x2;
302             if (x2max < (*it).x2) x2max = (*it).x2;
303             if (x3min >(*it).x3) x3min  = (*it).x3;
304             if (x3max < (*it).x3) x3max = (*it).x3;
305             if (x4min >(*it).x4) x4min  = (*it).x4;
306             if (x4max < (*it).x4) x4max = (*it).x4;
307         }
308         int vol = (x1max - x1min + 1) * (x2max - x2min + 1) * (x3max - x3min + 1) * (x4max - x4min + 1);
309         cout << vol << endl;
310     }
311     system("pause");
312     return 0;
313 }
View Code

转载于:https://www.cnblogs.com/dengeven/p/3228269.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值