1. 程式人生 > >HDU 1348 Wall 【凸包】

HDU 1348 Wall 【凸包】

ons algorithm 2.x ble https clu ostream %d font

<題目鏈接>

題目大意:

給出二維坐標軸上 n 個點,這 n 個點構成了一個城堡,國王想建一堵墻,城墻與城堡之間的距離總不小於一個數 L ,求城墻的最小長度,答案四舍五入.

解題分析:

求出這些點所圍成的凸包,然後所圍城墻的長度就為 該凸包周長 + 以該距離為半徑的圓的周長。具體證明如下:

技術分享圖片

下面的模板還沒有整理好

Graham 凸包算法

技術分享圖片
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using
namespace std; #define maxn 1100 const double pi = acos(-1.0); struct Point { int x, y; }s[maxn]; int st[maxn], top; int cross(Point p, Point p1, Point p2) { return (p1.x - p.x)*(p2.y - p.y) - (p2.x - p.x)*(p1.y - p.y); } double dist(Point p1, Point p2) { double tmp = (p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y);
return sqrt(tmp); } bool cmp(Point p1, Point p2) { int tmp = cross(s[0], p1, p2); if (tmp>0) return true; else if (tmp == 0 && dist(s[0], p1)<dist(s[0], p2)) return true; else return false; } void Graham(int n) { if (n == 1) { top = 0; st[0] = 0; } if (n == 2) { top = 1
; st[0] = 0; st[1] = 1; } if (n>2) { int i; st[0] = 0, st[1] = 1; top = 1; for (i = 2; i<n; i++) { while (top>0 && cross(s[st[top - 1]], s[st[top]], s[i])<0) //不習慣s[st[top-1]]這種代碼風格的可以改一下 top--; st[++top] = i; } } } int main() { int n, l; int ncase; cin >> ncase; while (ncase--) { scanf("%d %d", &n, &l); Point p0; scanf("%d%d", &s[0].x, &s[0].y); p0.x = s[0].x, p0.y = s[0].y; int k = 0; for (int i = 1; i < n; i++) { scanf("%d%d", &s[i].x, &s[i].y); if ((p0.y > s[i].y) || (p0.y == s[i].y&&p0.x > s[i].x)) { p0.x = s[i].x; p0.y = s[i].y; k = i; } } s[k] = s[0]; s[0] = p0; sort(s + 1, s + n, cmp); Graham(n); double ans = 0; for (int i = 0; i < top; i++) { ans += dist(s[st[i]], s[st[i + 1]]); } ans += dist(s[st[0]], s[st[top]]); ans += 2 * pi*l; printf("%d\n", (int)(ans + 0.5)); if (ncase)printf("\n"); } return 0; }
View Code

Andrew算法

技術分享圖片
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const double Pi=acos(-1.0);
struct Point{
    double x,y;
    Point(double x=0,double y=0):x(x),y(y){}
};
typedef Point Vector;
bool operator < (Point a,Point b){return a.x<b.x||(a.x==b.x&&a.y<b.y);}

Vector operator - (Point a,Point b){return Vector(a.x-b.x,a.y-b.y);}

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 Angle(Vector a,Vector b){return acos(Dot(a,b)/Length(a)/Length(b));}

Vector Rotate(Vector a,double rad){return Vector(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));}

double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}

Point operator + (Point a,Vector b){return Point(a.x+b.x,a.y+b.y);}

Point getdot(Point a,Vector b,double ang){return a+Rotate(b,ang);}

double getrad(double ang){return Pi*(ang/180);}

Point ans[2500],at[2500];
int nu;


double polygonArea(){
    int k=0;
    for(int i=0;i<nu;i++){
        while(k>1&&Cross(ans[k-1]-ans[k-2],at[i]-ans[k-2])<=0)k--;
        ans[k++]=at[i];
    }
    int p=k;
    for(int i=nu-1;i>=0;i--){
        while(k>p&&Cross(ans[k-1]-ans[k-2],at[i]-ans[k-2])<=0)k--;
        ans[k++]=at[i];
    }
    double x=0;
    k--;
    if(k<2)return 0;

    //求該凸多邊形面積
    for(int i=1;i<k-1;i++)x+=Cross(ans[i]-ans[0],ans[i+1]-ans[0]);
    return x/2;
}


int main(){
    int T,n;
    double x,y,w,h,ang;
    scanf("%d",&T);
    while(T--){
        double area1=0,area2=0;
        nu=0;
        scanf("%d",&n);
        while(n--){
            scanf("%lf%lf%lf%lf%lf",&x,&y,&w,&h,&ang);
            area2+=w*h;    //area儲存所有矩形的總面積
            Point a;
            ang=-getrad(ang);//因為是順時針旋轉的,所以要是負的。。。。。 
            at[nu++]=getdot(Point(x,y),Vector(w/2,h/2),ang);
            at[nu++]=getdot(Point(x,y),Vector(-w/2,h/2),ang);
            at[nu++]=getdot(Point(x,y),Vector(w/2,-h/2),ang);
            at[nu++]=getdot(Point(x,y),Vector(-w/2,-h/2),ang);
        }
        sort(at,at+nu);
        area1=polygonArea();
    //    printf("%lf %lf\n",area1,area2);
        printf("%.1lf %%\n",100*area2/area1);
    }
    return 0;
}
View Code

2018-08-04

HDU 1348 Wall 【凸包】