首页 > 代码库 > UVa 1009 Balloons in a Box

UVa 1009 Balloons in a Box

方法:暴力 枚举

数据量较小,可以枚举所有n!个order,然后依次计算该order所对应的体积,更新答案。因为没有剪枝,所以用next_permutation 列出所有可能性即可。

code:

技术分享
  1 #include <cstdio>
  2 #include <cstring>
  3 #include <algorithm>
  4 #include <iostream>
  5 #include <string>
  6 #include <vector>
  7 #include <stack>
  8 #include <bitset>
  9 #include <cstdlib>
 10 #include <cmath>
 11 #include <set>
 12 #include <list>
 13 #include <deque>
 14 #include <map>
 15 #include <queue>
 16 #include <fstream>
 17 #include <cassert>
 18 #include <unordered_map>
 19 #include <unordered_set>
 20 #include <cmath>
 21 #include <sstream>
 22 #include <time.h>
 23 #include <complex>
 24 #include <iomanip>
 25 #define Max(a,b) ((a)>(b)?(a):(b))
 26 #define Min(a,b) ((a)<(b)?(a):(b))
 27 #define FOR(a,b,c) for (ll (a)=(b);(a)<(c);++(a))
 28 #define FORN(a,b,c) for (ll (a)=(b);(a)<=(c);++(a))
 29 #define DFOR(a,b,c) for (ll (a)=(b);(a)>=(c);--(a))
 30 #define FORSQ(a,b,c) for (ll (a)=(b);(a)*(a)<=(c);++(a))
 31 #define FORC(a,b,c) for (char (a)=(b);(a)<=(c);++(a))
 32 #define FOREACH(a,b) for (auto &(a) : (b))
 33 #define rep(i,n) FOR(i,0,n)
 34 #define repn(i,n) FORN(i,1,n)
 35 #define drep(i,n) DFOR(i,n-1,0)
 36 #define drepn(i,n) DFOR(i,n,1)
 37 #define MAX(a,b) a = Max(a,b)
 38 #define MIN(a,b) a = Min(a,b)
 39 #define SQR(x) ((LL)(x) * (x))
 40 #define Reset(a,b) memset(a,b,sizeof(a))
 41 #define fi first
 42 #define se second
 43 #define mp make_pair
 44 #define pb push_back
 45 #define all(v) v.begin(),v.end()
 46 #define ALLA(arr,sz) arr,arr+sz
 47 #define SIZE(v) (int)v.size()
 48 #define SORT(v) sort(all(v))
 49 #define REVERSE(v) reverse(ALL(v))
 50 #define SORTA(arr,sz) sort(ALLA(arr,sz))
 51 #define REVERSEA(arr,sz) reverse(ALLA(arr,sz))
 52 #define PERMUTE next_permutation
 53 #define TC(t) while(t--)
 54 #define forever for(;;)
 55 #define PINF 1000000000000
 56 #define newline ‘\n‘
 57 
 58 #define test if(1)if(0)cerr
 59 using namespace std;
 60 using namespace std;
 61 typedef vector<int> vi;
 62 typedef vector<vi> vvi;
 63 typedef pair<int,int> ii;
 64 typedef pair<double,double> dd;
 65 typedef pair<char,char> cc;
 66 typedef vector<ii> vii;
 67 typedef long long ll;
 68 typedef unsigned long long ull;
 69 typedef pair<ll, ll> l4;
 70 const double pi = acos(-1.0);
 71 
 72 struct Point
 73 {
 74     double x[3];
 75 };
 76 double distance(const Point &l, const Point &r)
 77 {
 78     double ret = 0;
 79     rep(i, 3)
 80     {
 81         double d = l.x[i]-r.x[i];
 82         ret += d*d;
 83     }
 84     return sqrt(ret);
 85 }
 86 Point v[2];
 87 Point center[6];
 88 double r[6];
 89 int perm[6] = {0,1,2,3,4,5};
 90 int n;
 91 double ans;
 92 void dfs(int pos, double vol)
 93 {
 94     int cur = perm[pos];
 95     if (pos == n)
 96     {
 97         ans = max(ans, vol);
 98         return;
 99     }
100     double tmp = abs(v[0].x[0]-center[cur].x[0]);
101     rep(i, 2) rep(j, 3)
102     tmp = min(tmp, abs(v[i].x[j]-center[cur].x[j]));
103     rep(i, pos)
104     {
105         if (!r[perm[i]]) continue;
106         tmp = min(tmp, distance(center[perm[i]], center[cur])-r[perm[i]]);
107     }
108     if (tmp <= 0)
109     {
110         r[cur] = 0;
111     }
112     else
113         r[cur] = tmp;
114     dfs(pos+1, vol + pi * 4 / 3 * r[cur]*r[cur]*r[cur]);
115 }
116 int main()
117 {
118     ios::sync_with_stdio(false);
119     cin.tie(0);
120     int kase = 0;
121     while (cin >> n && n)
122     {
123         
124         rep(i, 2)
125         rep(j, 3) cin >> v[i].x[j];
126         rep(i, n)
127         rep(j, 3) cin >> center[i].x[j];
128         ans = 0;
129         do
130         {
131             Reset(r, 0);
132             dfs(0, 0);
133         } while (next_permutation(perm, perm+n));
134         double volume = 1;
135         rep(i, 3) volume *= v[1].x[i]-v[0].x[i];
136         volume = abs(volume);
137         ans = volume-ans;
138         cout << "Box " << ++kase << ": " << (int) (ans + 0.5) << newline << newline;;
139     }
140 
141 }
142 
143 /*
144  2
145  0 0 0
146  10 10 10 
147  3 3 3 
148  7 7 7
149  0
150 */
View Code

 

UVa 1009 Balloons in a Box