盒子里的气球(World Finals 2002 Balloons in a Box) 附下福州大学的oj上的链接 http://acm.fzu.edu.cn/problem.php?pid=1515 #include<stdio.h> #include<string.h> #include<math.h> #include<stdlib.h> #include<algorithm> using namespace std; const int maxn = 10; struct point { int x, y, z; }; struct balloon { int x, y, z; int d_wall; double r; }; balloon b[maxn]; point box0, box1; int idx[maxn]; bool vis[maxn]; int n; double v; double ans; const double pi = acos(-1.0); const double inf = 10000000000.0; double volume() { return fabs((double)(box1.x-box0.x) * (box1.y-box0.y) * (box1.z-box0.z)); } double dist(int idx1, int idx2) { return sqrt((double)(b[idx1].x-b[idx2].x)*(b[idx1].x-b[idx2].x) + (double)(b[idx1].y-b[idx2].y)*(b[idx1].y-b[idx2].y) + (double)(b[idx1].z-b[idx2].z)*(b[idx1].z-b[idx2].z)); } void dfs(int step, double vol) { if(step == n) { // printf("%.2lf %.2lf/n", v, vol); if(ans > v - vol) ans = v-vol; return ; } else { for(int i = 0; i < n; i++) if(!vis[i]) { vis[i] = true; double r = b[i].d_wall; // printf("%.2lf/n", r); for(int j = 0; j < n; j++) if(i != j && vis[j]) r = min(r, dist(i, j)-b[j].r); b[i].r = r; // printf("%.2lf/n", b[i].r); if(r > 0) dfs(step+1, vol + 4.0*pi*r*r*r/3); else dfs(step+1, vol); vis[i] = false; } } } #define LOCAL int main() { #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif while(scanf("%d", &n) == 1) { scanf("%d%d%d", &box0.x, &box0.y, &box0.z); scanf("%d%d%d", &box1.x, &box1.y, &box1.z); // printf("%.2lf %.2lf %.2lf/n", box0.x, box0.y, box0.z); // printf("%.2lf %.2lf %.2lf/n", box1.x, box1.y, box1.z); for(int i = 0; i < n; i++) { scanf("%d%d%d", &b[i].x, &b[i].y, &b[i].z); b[i].d_wall = min(abs(box0.x-b[i].x), abs(box1.x-b[i].x)); b[i].d_wall = min(b[i].d_wall, abs(box0.y-b[i].y)); b[i].d_wall = min(b[i].d_wall, abs(box1.y-b[i].y)); b[i].d_wall = min(b[i].d_wall, abs(box0.z-b[i].z)); b[i].d_wall = min(b[i].d_wall, abs(box1.z-b[i].z)); b[i].r = 0; idx[i] = i; } v = volume(); ans = inf; // printf("%.2lf/n", v); memset(vis, false, sizeof(vis)); dfs(0, 0.0); printf("%.lf/n", ans); } }