首页 > POJ1022 Packing Unit 4D Cubes

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 
  8 #include 
  9 #include 
 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 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::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::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::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::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::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::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::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::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::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::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::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::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::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::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::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::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::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

更多相关:

  • #include #include #include #include #include #include #include

  • 题目:表示数值的字符串 请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100"、"5e2"、"-123"、"3.1416"、"0123"及"-1E-16"都表示数值,但"12e"、"1a3.14"、"1.2.3"、"+-5"及"12e+5.4"都不是。 解题: 数值错误的形式有多种多样,但是正确的...

  • 加法伺候  //超过20位数值相加---------------------------------------- function bigNumAdd(a, b) {if (!(typeof a === "string" && typeof b === "string")) return console.log("传入参数必...

  • 业务场景: 从中文字句中匹配出指定的中文子字符串 .这样的情况我在工作中遇到非常多, 特梳理总结如下. 难点: 处理GBK和utf8之类的字符编码, 同时正则匹配Pattern中包含汉字,要汉字正常发挥作用,必须非常谨慎.推荐最好统一为utf8编码,如果不是这种最优情况,也有酌情处理. 往往一个具有普适性的正则表达式会简化程...

  • 简单record 一下 #include // 'struct sockaddr_in' #include #include // 'struct ifreq' and 'struct if_nameindex' #include #inc...

  •         Apache POI是一个开源的利用Java读写Excel,WORD等微软OLE2组件文档的项目。        我的需求是对Excel的数据进行导入或将数据以Excel的形式导出。先上简单的测试代码:package com.xing.studyTest.poi;import java.io.FileInputSt...

  • 要取得[a,b)的随机整数,使用(rand() % (b-a))+ a; 要取得[a,b]的随机整数,使用(rand() % (b-a+1))+ a; 要取得(a,b]的随机整数,使用(rand() % (b-a))+ a + 1; 通用公式:a + rand() % n;其中的a是起始值,n是整数的范围。 要取得a到b之间的...

  • 利用本征图像分解(Intrinsic Image Decomposition)算法,将图像分解为shading(illumination) image 和 reflectance(albedo) image,计算图像的reflectance image。 Reflectance Image 是指在变化的光照条件下能够维持不变的图像部分...

  • 题目:面试题39. 数组中出现次数超过一半的数字 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 示例 1: 输入: [1, 2, 3, 2, 2, 2, 5, 4, 2] 输出: 2 限制: 1 <= 数组长度 <= 50000 解题: cl...

  • 题目:二叉搜索树的后序遍历序列 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历结果。如果是则返回 true,否则返回 false。假设输入的数组的任意两个数字都互不相同。 参考以下这颗二叉搜索树:      5     /    2   6   /  1   3示例 1: 输入: [1,6,3,2,5] 输出...