How Big Is It? |
Ian's going to California, and he has to pack his things, including his collection of circles. Given a set of circles, your program must find the smallest rectangular box in which they fit. All circles must touch the bottom of the box. The figure below shows an acceptable packing for a set of circles (although this may not be the optimal packing for these particular circles). Note that in an ideal packing, each circle should touch at least one other circle (but you probably figured that out).
The first line of input contains a single positive decimal integer n , n <=50. This indicates the number of lines which follow. The subsequent n lines each contain a series of numbers separated by spaces. The first number on each of these lines is a positive integer m , m <=8, which indicates how many other numbers appear on that line. The next m numbers on the line are the radii of the circles which must be packed in a single box. These numbers need not be integers.
For each data line of input, excluding the first line of input containing n , your program must output the size of the smallest rectangle which can pack the circles. Each case should be output on a separate line by itself, with three places after the decimal point. Do not output leading zeroes unless the number is less than 1, e.g. 0.543
.
3 3 2.0 1.0 2.0 4 2.0 2.0 2.0 2.0 3 2.0 1.0 4.0
9.657 16.000 12.657
题意。。给定t组数据。每组数据包含一个n。然后输入n个圆的半径。。
然后要计算出。最小需要多长的盒子。才能把所有圆横着放满。。
思路:暴力枚举。。由于圆最多8个。完全可以暴力。。。
就是计算长度的时候麻烦点。。我的方法是:保存下每个圆的横坐标。。圆的横坐标加半径。最大的就是当前情况所需要的盒子大小。在从所有情况中找出最小的。。。每个圆放进去。都必定会与一个圆或者左边的墙壁相切。所以找出当前圆和前面所有放置的圆的横坐标最大的就是当前圆的横坐标。。
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h> #include <math.h> #include <iostream> #include <algorithm> using namespace std; int t; int n; double minn; struct Y { double x; double r; } yuan[10]; bool cmp(Y a, Y b) { return a.r < b.r; } void find() { double you = 0; for (int i = 1; i <= n; i ++) { double maxx = 0; for (int j = 0; j < i; j ++) { double dd; if (j == 0) dd = yuan[i].r; else dd = sqrt((yuan[i].r + yuan[j].r) * (yuan[i].r + yuan[j].r) - (yuan[i].r - yuan[j].r) * (yuan[i].r - yuan[j].r)); if (maxx < dd + yuan[j].x) { maxx = dd + yuan[j].x; } } yuan[i].x = maxx; } double sb = 0; for (int i = 1; i <= n; i ++) { if (sb < yuan[i].x + yuan[i].r) { sb = yuan[i].x + yuan[i].r; } } if (minn > sb) minn = sb; } int main() { scanf("%d", &t); while (t --) { minn = 999999999; memset(yuan, 0, sizeof(yuan)); scanf("%d", &n); for (int i = 1; i <= n; i ++) { scanf("%lf", &yuan[i].r); } sort(yuan + 1, yuan + n + 1, cmp); find(); while (next_permutation(yuan + 1, yuan + n + 1, cmp)) { for (int i = 0; i <= n; i ++) { yuan[i].x = 0; } find(); } printf("%.3lf\n", minn); } return 0; }