最近开始重读刘汝佳的黑书,从最开始的算法开始吧,毕竟好久没搞了,废话不多说,我们来看看枚举吧

关于枚举的说明,大家可以看看刘汝佳老师的《算法艺术及信息学竞赛》和配套课件,我就不多说了

UVA1009

链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=245&page=show_problem&problem=3450

很基础的题目,但还是wa了好几次

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<string>
 5 #include<cmath>
 6 #include<vector>
 7 #include<algorithm>
 8 #include<map>
 9 using namespace std;
10 #define pi acos(-1.0)
11 const int maxn=15;
12 double sx,sy,sz,fx,fy,fz,r[maxn],maxv;
13 int n,sel[maxn],vist[maxn];
14 struct point
15 {
16     double x,y,z;
17 };
18 point p[maxn];
19 double dist(int a,int b)  //两点之间的距离
20 {
21     return sqrt((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y)+(p[a].z-p[b].z)*(p[a].z-p[b].z));
22 }
23 double distege(int i)    //到每条边之间距离的最小值
24 {
25     double a,b,c;
26     a=min(fabs(p[i].x-sx),fabs(p[i].x-fx));
27     b=min(fabs(p[i].y-sy),fabs(p[i].y-fy));
28     c=min(fabs(p[i].z-sz),fabs(p[i].z-fz));
29     return min(a,min(b,c));
30 }
31 void distmin()  //计算距离的半径的值
32 {
33     double a,v=0;
34     for(int i=0;i<n;i++)
35     {
36         a=distege(sel[i]);
37         for(int j=0;j<i;j++)
38         {
39             a=min(a,dist(sel[i],sel[j])-r[sel[j]]);
40         }
41         r[sel[i]]=(a<0)? 0:a;
42         if(a<=0)  continue;
43         v+=(4.0/3*pi*r[sel[i]]*r[sel[i]]*r[sel[i]]);
44     }
45     maxv=max(maxv,v);
46 }
47 void dfs(int cnt)
48 {
49     if(cnt==n)
50         distmin();
51     for(int i=0;i<n;i++)
52     {
53         if(vist[i])  continue;
54         sel[cnt]=i;
55         vist[i]=1;
56         dfs(cnt+1);
57         vist[i]=0;
58     }
59 }
60 int main()
61 {
62     int te=1;
63     while(cin>>n)
64     {
65         if(n==0)   break;
66         cin>>sx>>sy>>sz>>fx>>fy>>fz;
67         for(int i=0;i<n;i++)
68             cin>>p[i].x>>p[i].y>>p[i].z;
69             maxv=0;
70         memset(vist,0,sizeof(vist));
71         dfs(0);
72         printf("Box %d: %.0lf\n\n",te++,((fabs((sx-fx)*(sy-fy)*(sz-fz)))-maxv));
73     }
74     return 0;
75 }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-22
  • 2021-09-23
  • 2021-07-12
  • 2021-12-31
  • 2021-07-08
猜你喜欢
  • 2018-03-01
  • 2021-11-02
  • 2022-12-23
  • 2022-01-16
相关资源
相似解决方案