题解:【P3958】 奶酪
题目:
传送门:https://www.luogu.org/problem/P3958
AC代码:DFS
1 #include<iostream> 2 #include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<cmath> 6 using namespace std; 7 int n,outfg,cnt,anst; 8 double h,r; 9 int ans[1005]; 10 struct node 11 { 12 double x; 13 double y; 14 double z; 15 int vis; 16 }p[1005]; 17 bool cmp(node a,node b) 18 { 19 return a.z<b.z; 20 } 21 double dis(node a,node b) 22 { 23 double x1=a.x, x2=b.x, y1=a.y, y2=b.y, z1=a.z, z2=b.z; 24 return sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)+(z2-z1)*(z2-z1)); 25 } 26 void dfs(int x) 27 { 28 if(p[x].z+r>=h)//边界 29 { 30 outfg=1; 31 return; 32 } 33 p[x].vis=1; 34 for(int i=1;i<=n;i++) 35 { 36 double d=dis(p[x],p[i]); 37 if(d==0) 38 continue; 39 if(outfg==1) 40 return ; 41 if(p[i].vis==0) 42 { 43 if(d<=r*2) 44 { 45 dfs(i); 46 } 47 } 48 } 49 } 50 int main() 51 { 52 int t; 53 scanf("%d",&t); 54 anst=t; 55 while(t--) 56 { 57 outfg=0; 58 scanf("%d %lf %lf ",&n,&h,&r); 59 for(int i=1;i<=n;i++) 60 p[i].vis=0; 61 for(int i=1;i<=n;i++) 62 { 63 scanf("%lf %lf %lf",&p[i].x,&p[i].y,&p[i].z); 64 } 65 sort(p+1,p+n+1,cmp); 66 for(int i=1;i<=n;i++) 67 { 68 if(outfg==1) 69 break; 70 if(p[i].z<=r) 71 dfs(i); 72 } 73 if(outfg==1) 74 ans[anst-t]=1; 75 else ans[anst-t]=0; 76 } 77 for(int i=1;i<=anst;i++) 78 if(ans[i]==1) 79 printf("Yes\n"); 80 else if(ans[i]==0) 81 printf("No\n"); 82 return 0; 83 }
重点(坑):
1.别闲的没事干往函数里面传结构体
个人理解传结构体原理是先将结构体复制一遍再进行操作,原结构体数据不改变(debug : 2.5h)
2.别清除访问标记
亲测导致部分数据无限循环//50%AC,50%TLE(debug:0.5h)
//噎死纪念
原文地址:https://www.cnblogs.com/randomaddress/p/11616491.html
时间: 2024-11-06 03:53:48