Wall - POJ 1113(求凸包)

题目大意:给N个点,然后要修建一个围墙把所有的点都包裹起来,但是要求围墙距离所有的点的最小距离是L,求出来围墙的长度。

分析:如果没有最小距离这个条件那么很容易看出来是一个凸包,然后在加上一个最小距离L,那么就是在凸包外延伸长度为L,如下图,很明显可以看出来多出来的长度就是半径为L的圆的周长,所以总长度就是凸包的周长+半径为L的圆的周长。

代码如下:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<string>
#include<vector>
#include<math.h>
using namespace std;

const double EPS = 1e-10;
const double PI = acos(-1);
const int MAXN = 1e3+7;
int sta[MAXN], top;
int Sign(double t)
{
    if(t > EPS)return 1;
    if(fabs(t) < EPS)return 0;
    return -1;
}
struct point
{
    double x, y;
    point(double x=0, double y=0):x(x), y(y){}
    point operator - (const point &t)const{
        return point(x-t.x, y-t.y);
    }
    double operator ^(const point &t)const{
        return x*t.y - y*t.x;
    }
    double operator *(const point &t)const{
        return x*t.x + y*t.y;
    }
}p[MAXN];
double Dist(point a, point b)
{
    return sqrt((a-b)*(a-b));
}
bool cmp(point a, point b)
{
    int t = Sign((a-p[0])^(b-p[0]));

    if(t == 0)
        return Dist(a, p[0]) < Dist(b, p[0]);
    return t > 0;
}
///求凸包
void Graham(int N)
{///注意是否有1和2的情况,这个题目要求的
    sta[0]=0, sta[1]=1, top=1;

    for(int i=2; i<N; i++)
    {
        while(top>0 && Sign((p[i]-p[sta[top]])^(p[sta[top-1]]-p[sta[top]])) <= 0)
            top--;
        sta[++top] = i;
    }
}

int main()
{
    int N, L;

    while(scanf("%d%d", &N, &L) != EOF)
    {
        int i, k=0;

        for(i=0; i<N; i++)
        {
            scanf("%lf%lf", &p[i].x, &p[i].y);
            if(p[k].y>p[i].y || (p[k].y==p[i].y && p[k].x>p[i].x))
                k = i;
        }
        swap(p[0], p[k]);
        sort(p+1, p+N, cmp);
        Graham(N);

        double ans = Dist(p[sta[0]],p[sta[top]]) + 2*PI*L;

        for(int i=0; i<top; i++)
            ans += Dist(p[sta[i]], p[sta[i+1]]);

        printf("%d\n", (int)(ans+0.5));
    }

    return 0;
}

  

时间: 2024-12-24 23:27:05

Wall - POJ 1113(求凸包)的相关文章

Wall POJ - 1113 (凸包周长)

题目链接:https://vjudge.net/problem/POJ-1113 题目: 思路:就是求凸包周长加上一个圆周长 原文地址:https://www.cnblogs.com/Vampire6/p/12234411.html

POJ 1113 Wall(Graham求凸包周长)

题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <cmath> 5 #include <algorithm> 6 7 const double PI = acos(-1.0) ;

POJ 2187 求凸包上最长距离

简单的旋转卡壳题目 以每一条边作为基础,找到那个最远的对踵点,计算所有对踵点的点对距离 这里求的是距离的平方,所有过程都是int即可 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std; 8 #

POJ 1113 Wall 卷包裹法求凸包

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31199   Accepted: 10521 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w

poj 1113 Wall(标准的凸包果题)

题目链接:http://poj.org/problem?id=1113 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he would not listen to his Architect's proposals to build

poj 1113 Wall (凸包模板题)

Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32808   Accepted: 11137 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's castle. The King was so greedy, that he w

【POJ 1113】 Wall (凸包)

[POJ 1113] Wall 给n个点 连出一个凸包 然后在凸包外筑墙 要求墙与凸包每一处的距离都>=l 问需要建的最短的墙长 乍一看挺难 画画图就能看出来 凸包外建距离l的墙 其实就是在凸包每个顶点处 以顶点为圆心 做半径为l的弧 做到两侧半径与点的两边平行即可 然后把这些弧都用直线衔接 就是最短墙长 这样还不好求 呢把弧拿出来呢 其实就相当于把整个凸包作为一个点 以该点为圆心 l为半径做了个圆 这样弧的总长就是2*PI*l 那剩下的就是直线 平移下来其实就是凸包的周长 然后卷包裹法或者扫描

POJ 1113 || HDU 1348: wall(凸包问题)

传送门: POJ:点击打开链接 HDU:点击打开链接 下面是POJ上的题: Wall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29121   Accepted: 9746 Description Once upon a time there was a greedy King who ordered his chief Architect to build a wall around the King's cast

POJ 1113 Wall (凸包)

题目地址:POJ 1113 先求出凸包的周长,然后剩下的弧合起来一定是个半径为l的圆,然后再加上以l为半径的圆的周长即可. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <stdlib.h> #include <math.h> #include <ctype.h> #include <