TopCoder problem "CuboidJoin" used in SRM 191 (Division II Level Three)



Problem Statement

    A cuboid is a rectangular solid. Given a few cuboids, which may or may not overlap, you have to find out the total volume enclosed by them. A volume is considered enclosed if it falls completely within at least one of the specified cuboids. A volume that is in between several cuboids, but not inside any of them, is not considered enclosed.



The input is given as a int[], cuboids. The first 6 elements of cuboids describe the first cuboid, the next 6 the second cuboid and so on. The 6 elements specify the left-bottom-front vertex and then the right-top-back vertex. Each vertex is specified by 3 elements which refer to its x, y and z coordinates respectively.



For instance, if cuboids = {0,0,0,1,1,1}, it would mean there is one cuboid whose left-bottom-front vertex is (0,0,0) and whose right-top-back vertex is (1,1,1). In this case the volume enclosed would be 1. However, if cuboids = {0,0,0,1,1,1,1,1,1,2,2,2}, it would mean there are two cuboids, one whose left-bottom-front vertex is (0,0,0) and right-top-back vertex is (1,1,1), and another whose left-bottom-front vertex is (1,1,1) and right-top-back vertex is (2,2,2). In this case, the enclosed volume would be 2.
 

Definition

    
Class:CuboidJoin
Method:totalVolume
Parameters:int[]
Returns:long
Method signature:long totalVolume(int[] cuboids)
(be sure your method is public)
    
 

Notes

-The cuboids are always aligned with the grid. In other words, the cuboids are never tilted or twisted with respect to the axes.
-The x coordinate increases from left to right, the y coordinate from bottom to top, and the z coordinate from front to back.
 

Constraints

-cuboids will contain between 0 and 30 elements, inclusive.
-The number of elements in cuboids will be a multiple of 6.
-Each element of cuboids will be between -5000 and 5000, inclusive.
-As is explained in the problem statement, each cuboid is specified as a block of six elements x1,y1,z1,x2,y2,z2. In each such block, x1 is less than or equal to x2, y1 is less than or equal to y2 and z1 is less than or equal to z2.
 

Examples

0)
    
{0,0,0,1,1,1}
Returns: 1
There is just one cuboid which is a 1-by-1-by-1 cube. Hence, the volume enclosed is 1*1*1 = 1.
1)
    
{0,0,0,1,1,1,1,1,1,2,2,2}
Returns: 2
There are two cuboids, both of which are 1-by-1-by-1 cubes. Hence, the volume enclosed is 1*1*1 + 1*1*1 = 2.
2)
    
{0,0,0,4,4,4,0,0,0,1,1,1}
Returns: 64
There are two cuboids. One is a 1-by-1-by-1 cube and another is a 4-by-4-by-4 cube. The volume enclosed is 4*4*4 = 64 as the 1-by-1-by-1 cube is completely enclosed within the 4-by-4-by-4 cube.
3)
    
{-5000,-5000,-5000,5000,5000,5000}
Returns: 1000000000000
4)
    
{0,0,0,1,2,3,5,5,5,6,6,6}
Returns: 7
5)
    
{}
Returns: 0
6)
    
{0,0,0,1,1,0}
Returns: 0
The answer is 0 because "flat" solids enclose no volume.

Problem url:

http://www.topcoder.com/stat?c=problem_statement&pm=2329

Problem stats url:

http://www.topcoder.com/tc?module=ProblemDetail&rd=4775&pm=2329

Writer:

Ishan

Testers:

lbackstrom , vorthys

Problem categories:

Geometry, Math