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

题目大意:

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

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

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


Sample Input

  1. 1
  2. 9
  3. 1 2 3 4 5 6 7 8 9
  4. 2 0 1 0 0 0 0 0 0
  5. 3 1 0 0 0 0 0 0 0
  6. 4 0 0 0 1 0 0 0 0
  7. 5 0 0 1 0 0 0 0 0
  8. 6 0 0 0 0 0 1 0 0
  9. 7 0 0 0 0 1 0 0 0
  10. 8 0 0 0 0 0 0 0 1
  11. 9 0 0 0 0 0 0 1 0

Sample Output

  1. 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 = ; caseNo <= ncase; ++caseNo) {
  27. int n;
  28. map<int, Cube> cubes;
  29. cin >> n;
  30. for (int i = ; 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 = ;
  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 != ) {
  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 + ;
  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 != ) {
  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 - ;
  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 != ) {
  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 + ;
  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 != ) {
  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 - ;
  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 != ) {
  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 + ;
  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 != ) {
  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 - ;
  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 != ) {
  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 + ;
  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 != ) {
  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 - ;
  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 = ;
  291. int x1max = -;
  292. int x2min = ;
  293. int x2max = -;
  294. int x3min = ;
  295. int x3max = -;
  296. int x4min = ;
  297. int x4max = -;
  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 + ) * (x2max - x2min + ) * (x3max - x3min + ) * (x4max - x4min + );
  309. cout << vol << endl;
  310. }
  311. system("pause");
  312. return ;
  313. }

POJ1022 Packing Unit 4D Cubes的更多相关文章

  1. POJ题目排序的Java程序

    POJ 排序的思想就是根据选取范围的题目的totalSubmittedNumber和totalAcceptedNumber计算一个avgAcceptRate. 每一道题都有一个value,value ...

  2. Daily Query

    -- GI Report SELECT A.PLPKLNBR, D.DNDNHNBR, F.DNSAPCPO, C.PPPRODTE, A.GNUPDDTE GI_DATE, B.INHLDCDE, ...

  3. 【Moqui业务逻辑翻译系列】Shipment Receiver Receives Shipment with Packing Slip but no PO

    Shipment Receiver receives shipment. It has invoice tucked into it. Receiver records vendor name, ve ...

  4. ABP(现代ASP.NET样板开发框架)系列之12、ABP领域层——工作单元(Unit Of work)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之12.ABP领域层——工作单元(Unit Of work) ABP是“ASP.NET Boilerplate Pr ...

  5. ABP源码分析十:Unit Of Work

    ABP以AOP的方式实现UnitOfWork功能.通过UnitOfWorkRegistrar将UnitOfWorkInterceptor在某个类被注册到IOCContainner的时候,一并添加到该类 ...

  6. Failed to stop iptables.service: Unit iptables.service not loaded.

    redhat 7 [root@lk0 ~]# service iptables stop Redirecting to /bin/systemctl stop iptables.service Fai ...

  7. 4D卓越团队-两天培训总结

    上周末参加了公司组织的领导力培训课程-4D卓越团队(创业型团队领导力训练项目),感觉有一些用,在这里分享一下. 课前游戏 培训老师先带我们做了一个游戏:每一个人,在同时参加培训的人中找到另外的 6 个 ...

  8. VS2012 Unit Test 个人学习汇总(含目录)

    首先,给出MSDN相关地址:http://msdn.microsoft.com/en-us/library/Microsoft.VisualStudio.TestTools.UnitTesting.a ...

  9. VS2012 Unit Test —— 我对IdleTest库动的大手术以及对Xml相关操作进行测试的方式

    [1]我的IdleTest源码地址:http://idletest.codeplex.com/ [2]IdleTest改动说明:2013年10月份在保持原有功能的情况下对其动了较大的手术,首先将基本的 ...

随机推荐

  1. 股神小D

    题目大意: 给定一棵树,每一条边有$L,R$两种权值,求有多少条路径满足$\max(L)\leq\min(R)$. 解法$1-$点分治$+$二维数点 统计树上的路径应首先想到点分治,我们很显然可以搜出 ...

  2. bzoj 2631: tree link-cut-tree

    题目: Description 一棵n个点的树,每个点的初始权值为1.对于这棵树有q个操作,每个操作为以下四种操作之一: + u v c:将u到v的路径上的点的权值都加上自然数c: - u1 v1 u ...

  3. 【LeetCode】023. Merge k Sorted Lists

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...

  4. Cannot resolve class or package 'springframework' less... (Ctrl+F1) Inspection info:Spring XML mode

    其实这个问题是由于MySQL 这个jar 包依赖类型默认是runtime ,也就是说只有运行时生效,所以虽然这里报错,但是不影响你代码运行. 解决方案: 将runtime 修改为Compile 即可 ...

  5. 2018.10.30 一题 洛谷4660/bzoj1168 [BalticOI 2008]手套——思路!问题转化与抽象!+单调栈

    题目:https://www.luogu.org/problemnew/show/P4660 https://www.lydsy.com/JudgeOnline/problem.php?id=1168 ...

  6. HDOJ1073(gets 应用)

    练习操作字符串的好题. #include<cstdio> #include<algorithm> #include<cstring> using namespace ...

  7. Project Online JS 添加Ribbon按钮

    var Projects = Projects || {}; (function () { Projects.ribbonButtonClick = function (name) { var pro ...

  8. asp页面重定向

    asp页面重定向 1.当你点击某页面时(没有登录),而此页面需要登录,登录后页面需要定向到你之前操作的页面时 就用到了重定向. 2.login.aspx?redirUrl="重定向的页面地址 ...

  9. 洛谷-关押罪犯-NOIP2010提高组复赛

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用“怨气值”(一个正整数值)来表示 ...

  10. 在64位ubuntu上安装alienbrain客户端

    一.首先从Alienbrain_EN_10.5.zip安装包(网上可搜索下载)里提取出linux版安装文件:Installations/Clients/Linux/NoVM/install.bin并c ...