(HDU1348)计算几何--Scan法求凸包

来源:互联网 发布:js怎么遍历对象数组 编辑:程序博客网 时间:2024/05/21 04:19

题目:HDU1348

题目链接:HDU1348-传送门

首次更改自己的代码风格,感觉很厉害。

Description:
没什么用

Input:
The first line of the input file contains two integer numbers N and L separated by a space. N (3 <= N <= 1000) is the number of
vertices in the King’s castle, and L (1 <= L <= 1000) is the minimal
number of feet that King allows for the wall to come close to the
castle.

Next N lines describe coordinates of castle’s vertices in a clockwise
order. Each line contains two integer numbers Xi and Yi separated by a
space (-10000 <= Xi, Yi <= 10000) that represent the coordinates of
ith vertex. All vertices are different and the sides of the castle do
not intersect anywhere except for vertices.

Output:

Write to the output file the single number that represents the minimal possible length of the wall in feet that could be built around
the castle to satisfy King’s requirements. You must present the
integer number of feet to the King, because the floating numbers are
not invented yet. However, you must round the result in such a way,
that it is accurate to 8 inches (1 foot is equal to 12 inches), since
the King will not tolerate larger error in the estimates.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line
followed by N input blocks. Each input block is in the format
indicated in the problem description. There is a blank line between
input blocks.

The output format consists of N output blocks. There is a blank line
between output blocks.

Sample Input
1

9 100
300 400
300 300
400 300
500 400
500 200
350 200
200 200

Sample Output
1628


题目概要:

这种题有种玩法叫做“看样例,懂题意”。这道题的意思看样例就可以了:多组数据,先输入数据组数;对于每组数据,先输入点的数目N,然后输入一个圆的半径L;然后输入N对数表示点的坐标,输出凸包的周长和圆的周长的和四舍五入到整数的结果。


思路:

很裸的凸包,主要看实现了,同时注意只有一个点或者只有两个点的情况。


代码:

#include<cstdio>#include<cstdlib>#include<cmath>#include<algorithm>#include<string>#include<cstring>#include<iostream>using namespace std;const double pi=acos(-1.0);const int maxn=int(1e3)+6;int n,tot;double r;double ans=0.0;struct Node {    double x,y;}p[maxn],P[maxn];double X(Node a,Node b,Node c) {    return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);}double lenth(Node a,Node b) {    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}bool cmp(Node a,Node b) {    double pp=X(p[0],a,b);    if(pp>0) return true;    if(pp<0) return false;    return lenth(p[0],a)<lenth(p[0],b);}int main() {    int cas;    scanf("%d",&cas);    for(int T=0;T<cas;T++) {        if(T!=0) putchar('\n');        scanf("%d%lf",&n,&r);        ans=pi*2.0*r;        for(int i=0;i<n;i++) scanf("%lf%lf",&p[i].x,&p[i].y);        if(n==1) {            printf("%.0lf\n",ans);            break;        }        else if(n==2) {            printf("%.0lf\n",ans+lenth(p[0],p[1]));            break;        }        for(int i=1;i<n;i++) if(p[i].y<p[0].y || (p[i].y==p[0].y && p[i].x<p[0].x))            swap(p[i],p[0]);        sort(p,p+n,cmp);        P[tot=0]=p[0];        P[++tot]=p[1];        for(int i=2;i<n;i++) {            while(tot>0 && X(P[tot-1],P[tot],p[i])<=0) tot--;            P[++tot]=p[i];        }        for(int i=0;i<tot;i++) ans+=lenth(P[i],P[i+1]);        ans+=lenth(P[0],P[tot]);        printf("%.0lf\n",ans);    }    return 0;}
1 0
原创粉丝点击