[ACM_几何] Wall
生活随笔
收集整理的這篇文章主要介紹了
[ACM_几何] Wall
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28417#problem/E
題目大意:依次給n個點圍成的一個城堡,在周圍建圍墻,要求圍墻離城墻的距離大于一定的值,求圍墻最短長度(結果四舍五入
解題思路:求圍住所有點的凸包周長+一個圓的周長
#include<iostream> #include<cmath> #include<string.h> #include<string> #include<stdio.h> #include<algorithm> #include<iomanip>using namespace std; #define eps 0.0000000001 #define PI acos(-1.0)//******************************************************************************* //點和向量 struct Point{double x,y;Point(double x=0,double y=0):x(x),y(y){} }; typedef Point Vector; Vector operator-(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);} bool operator<(const Vector& a,const Vector& b){return a.x<b.x||(a.x==b.x && a.y<b.y);} int dcmp(double x){if(fabs(x)<eps)return 0;else return x<0 ? -1:1; } double Dot(Vector A,Vector B){return A.x*B.x+A.y*B.y;}//向量點積 double Length(Vector A){return sqrt(Dot(A,A));}//向量模長 double Cross(Vector A,Vector B){return A.x*B.y-A.y*B.x;} //******************************************************************************** //計算凸包輸入點數組p,個數n,輸出點數組ch,返回凸包定點數 //輸入不能有重復,完成后輸入點順序被破壞 //如果不希望凸包的邊上有輸入點,把兩個<=改成< //精度要求高時,建議用dcmp比較 //基于水平的Andrew算法-->1、點排序2、刪除重復的然后把前兩個放進凸包 //3、從第三個往后當新點在凸包前進左邊時繼續,否則一次刪除最近加入的點,直到新點在左邊 int ConVexHull(Point* p,int n,Point*ch){sort(p,p+n);int m=0;for(int i=0;i<n;i++){//下凸包while(m>1 && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;ch[m++]=p[i];}int k=m;for(int i=n-2;i>=0;i--){//上凸包while(m>k && Cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<=0)m--;ch[m++]=p[i];}if(n>1)m--;return m; } //******************************************************************* int main(){int n,L;while(cin>>n>>L){Point p[1005],ch[1005];for(int i=0;i<n;i++) //輸入點cin>>p[i].x>>p[i].y;int m=ConVexHull(p,n,ch); //求解凸包并計算凸包周長double sum=Length(ch[0]-ch[m-1]);for(int i=1;i<m;i++)sum+=Length(ch[i]-ch[i-1]);printf("%.0lf\n", sum+2*L*PI);//輸出周長+圓周并四舍五入 }return 0; } View Code?
總結
以上是生活随笔為你收集整理的[ACM_几何] Wall的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 巧妙解决:access denied (
- 下一篇: VC中CCheckListBox使用注意