Problem
Balloons in a Box
Problem
You must write a program that simulates placing spherical balloons into a rectangular box.
The simulation scenario is as follows. Imagine that you are given a rectangular box and a set of points. Each point represents a position where you might place a balloon. To place a balloon at a point, center it at the point and inflate the balloon until it touches a side of the box or a previously placed balloon. You may not use a point that is outside the box or inside a previously placed balloon. However, you may use the points in any order you like, and you need not use every point. Your objective is to place balloons in the box in an order that maximizes the total volume occupied by the balloons.
You are required to calculate the volume within the box that is not enclosed by the balloons.
Input
The input consists of several test cases. The first line of each test case contains a single integer n that indicates the number of points in the set (1 <= n <= 6). The second line contains three integers that represent the (x, y, z) integer coordinates of a corner of the box, and the third line contains the (x, y, z) integer coordinates of the opposite corner of the box. The next n lines of the test case contain three integers each, representing the (x, y, z) coordinates of the points in the set. The box has non-zero length in each dimension and its sides are parallel to the coordinate axes.
The input is terminated by the number zero on a line by itself.
Output
For each test case print one line of output consisting of the test case number followed by the volume of the box not occupied by balloons. Round the volume to the nearest integer. Follow the format in the sample output given below.
Place a blank line after the output of each test case.
Sample Input
2 0 0 0 10 10 10 3 3 3 7 7 7 0
Output for the Sample Input
Box 1: 774
Solution
#include <fstream>
#include <vector>
#include <algorithm>
#include <limits>
#include <math.h> using namespace std; // 3D point struct Tuple { long x; long y; long z; // define operator < for use with sort & next_permutation algorithms bool operator < (const Tuple rhs) const { if (x < rhs.x && y < rhs.y && z < rhs.z) return true; return false; } }; // A balloon is a sphere with a center point and a radius struct Balloon { Tuple center; double size; }; // Balloons are put inside a rectangular box struct Box { Tuple corner1; Tuple corner2; }; typedef vector TupleVec; typedef vector BalVec; Box _box; // calculates the distance formula for two points // in three-dimensional space double Get3dDist(Tuple point1, Tuple point2) { double dx = point1.x - point2.x; double dy = point1.y - point2.y; double dz = point1.z - point2.z; return sqrt(dx*dx + dy*dy + dz*dz); } // checks to see if a particular 3D point falls inside of a // balloon/sphere. bool IsPointInBalloon(const BalVec& balloons, const Tuple point) { // for each of the balloons, take the point in question and see // if the distance between it and the center of the balloon is // less than the size of the balloon. If so, it falls inside the // balloon. for (BalVec::size_type i=0; i::max(); for (BalVec::size_type i=0; i curr_max_size) max_size2 = curr_max_size; } // take the lesser of the two values and make it the new size of the balloon. // add the balloon to a vector so it can be used for later calculation. Balloon balloon; balloon.center = point; balloon.size = min(max_size1, max_size2); balloons.push_back(balloon); return balloon.size; } int main() { long cases = 1; ifstream file; file.open("balloons.txt"); if (file.fail()) { cout << "Could not open balloons.txt" << endl; return false; } while (true) { // how many balloons to consider for this case? long num_points; file >> num_points; if (num_points == 0) // exit on reaching sentinel value of 0 return false; // read in opposite corners of the box file >> _box.corner1.x >> _box.corner1.y >> _box.corner1.z; file >> _box.corner2.x >> _box.corner2.y >> _box.corner2.z; // store the 3D points where the balloons are located vector points(num_points); for (long i=0; i> points[i].x >> points[i].y >> points[i].z; // vector must be sorted before next_permutation will // work correctly sort(points.begin(), points.end()); // calculate volume of cube long cube_vol = abs(_box.corner1.x - _box.corner2.x) * abs(_box.corner1.y - _box.corner2.y) * abs(_box.corner1.z - _box.corner2.z); // for each permutation of points, calculate the max possible size // of a sphere at the given point and subtract it from the remaining // volume in the cube double least_rem_vol = cube_vol; do { double curr_rem_vol = cube_vol; BalVec balloons; for (TupleVec::size_type i=0; i 0.5) least_rem_vol += 0.5; cout << "Box " << cases++ << ": " << (long)least_rem_vol << endl; } return 0; }