POJ 1113--Wall(计算凸包)

Wall
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 40363 Accepted: 13754
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 a beautiful brick wall with a perfect shape and nice tall towers. Instead, he ordered to build the wall around the whole castle using the least amount of stone and labor, but demanded that the wall should not come closer to the castle than a certain distance. If the King finds that the Architect has used more resources to build the wall than it was absolutely necessary to satisfy those requirements, then the Architect will loose his head. Moreover, he demanded Architect to introduce at once a plan of the wall listing the exact amount of resources that are needed to build the wall.

这里写图片描述

Your task is to help poor Architect to save his head, by writing a program that will find the minimum possible length of the wall that he could build around the castle to satisfy King's requirements.

The task is somewhat simplified by the fact, that the King's castle has a polygonal shape and is situated on a flat ground. The Architect has already established a Cartesian coordinate system and has precisely measured the coordinates of all castle's vertices in feet.
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.
Sample Input

9 100
200 400
300 400
300 300
400 300
400 400
500 400
500 200
350 200
200 200
Sample Output

1628
Hint

结果四舍五入就可以了

  • 不是直接求凸包,围住城堡的所需的最小距离,这个是凸包的长度,但是建造的围墙和城堡之间还有一个距离L,所以所求周长比凸包长度要多几段圆弧,所有圆弧的角度和为\(360°\),所以再加上一个半径为L的圆周长即为所求.
  • 步进法

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
const int MAX = 5005;
const double PI = acos(-1.0);
typedef struct point {
    double x;
    double y;
    point() {

    }
    point(double a, double b) {
        x = a;
        y = b;
    }
    point operator -(const point &b)const {     //返回减去后的新点
        return point(x - b.x, y - b.y);
    }
    double operator *(const point &b)const {    //点乘
        return x*b.x + y*b.y;
    }
}point;
double dist(point p1, point p2) {       //返回平面上两点距离
    return sqrt((p1 - p2)*(p1 - p2));
}
int n, c, w, ans[MAX], num, sd[MAX], ta, stk[MAX], tp1, tp2;
point x[MAX];
bool cmp(point a, point b) { return a.y < b.y || a.y == b.y && a.x < b.x; } //先按y,再按x从小排序
bool cmulti(point p1, point p2, point p3) {  //判断p1p0和p2p0的关系,<0,p1p0在p2p0的逆时针方向
    return ((p3.x - p1.x)*(p2.y - p1.y) - (p2.x - p1.x)*(p3.y - p1.y))<0;
}
void Jarvis() {
    ta = num = 0;
    sd[ta++] = 0, sd[ta++] = 1;
    sort(x, x + n, cmp);
    for (int i = 2; i < n; i++) {
        while (ta > 1 && !cmulti(x[sd[ta - 1]], x[sd[ta - 2]], x[i]))//不是外侧点则回溯,且不取内部点
            ta--;
        sd[ta++] = i;
    }
    for (int j = 0; j < ta; j++) {
        ans[num++] = sd[j];
    }
    ta = 0;
    sd[ta++] = n - 1;
    sd[ta++] = n - 2;
    for (int i = n - 3; i >= 0; i--) {
        while (ta > 1 && !cmulti(x[sd[ta - 1]], x[sd[ta - 2]], x[i]))
            ta--;
        sd[ta++] = i;
    }
    for (int j = 0; j < ta; j++) {
        ans[num++] = sd[j];
    }
}
int main() {
    scanf("%d%d", &n, &c);
    for (int i = 0; i < n; i++) {
        scanf("%lf%lf", &x[i].x, &x[i].y);
    }
    Jarvis();
    double res1 = 0;
    for (int i = 0; i < num - 1; i++) {
        res1 += dist(x[ans[i]], x[ans[i + 1]]);
    }
    res1 += 2 * PI * c;
    printf("%.0f\n", res1);
    return 0;
}
  • 用Gamham-scan重写了一下vo(*  ̄ ▽  ̄ *)ov

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
using namespace std;
const int MAX = 5005;
const double PI = acos(-1.0);
typedef struct point {
    double x;
    double y;
    point() {

    }
    point(double a, double b) {
        x = a;
        y = b;
    }
    point operator -(const point &b)const {     //返回减去后的新点
        return point(x - b.x, y - b.y);
    }
    double operator *(const point &b)const {    //点乘
        return x*b.x + y*b.y;
    }
}point;
double dist(point p1, point p2) {       //返回平面上两点距离
    return sqrt((p1 - p2)*(p1 - p2));
}
int n, res[MAX];    //ans为凸包点集坐标,n为点的个数,sd为临时坐标。
int top = 1;
point p[MAX];       //x存放凸包点集
bool cmp(point a, point b) {
    if (a.y == b.y) return a.x < b.x;
    return a.y < b.y;
}
bool multi(point p1, point p2, point p0) {  //判断p1p0和p2p0的关系,<0,p1p0在p2p0的逆时针方向,>0,p1p0在p2p0的顺时针方向
    return (p1.x - p0.x)*(p2.y - p0.y) >= (p2.x - p0.x)*(p1.y - p0.y);
}
void Graham(int n) {
    int i, len; //top模拟栈顶
    sort(p, p + n, cmp);
    //少于3个点也就没有办法形成凸包
    if (n == 0)return; res[0] = 0;
    if (n == 1)return; res[1] = 1;
    if (n == 2)return; res[2] = 2;
    for (i = 2; i < n; i++) {
        while (top&&multi(p[i], p[res[top]], p[res[top - 1]]))  //如果当前这个点和栈顶两个点构成折线右拐了,就回溯到上一个点
            top--;                                              //弹出栈顶
        res[++top] = i;                                         //否则将这个点入栈
    }
    len = top;
    res[++top] = n - 2;
    for (int i = n - 3; i >= 0; i--) {
        while (top!=len&&multi(p[i], p[res[top]], p[res[top - 1]]))
            top--;
        res[++top] = i;
    }
}
int main() {
    int c;
    scanf("%d%d", &n, &c);
        for (int i = 0; i < n; i++) {
            scanf("%lf%lf", &p[i].x, &p[i].y);
        }
        Graham(n);
        double res1 = 0;
        for (int i = 0; i < top; i++) {
            res1 += dist(p[res[i]], p[res[i + 1]]);
        }
        res1 += 2 * PI * c;
        printf("%.0f\n", res1);
    return 0;
}

转载于:https://www.cnblogs.com/FlyerBird/p/9385759.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园的建设目标是通过数据整合、全面共享,实现校园内教学、科研、管理、服务流程的数字化、信息化、智能化和多媒体化,以提高资源利用率和管理效率,确保校园安全。 智慧校园的建设思路包括构建统一支撑平台、建立完善管理体系、大数据辅助决策和建设校园智慧环境。通过云架构的数据中心与智慧的学习、办公环境,实现日常教学活动、资源建设情况、学业水平情况的全面统计和分析,为决策提供辅助。此外,智慧校园还涵盖了多媒体教学、智慧录播、电子图书馆、VR教室等多种教学模式,以及校园网络、智慧班牌、校园广播等教务管理功能,旨在提升教学品质和管理水平。 智慧校园的详细方案设计进一步细化了教学、教务、安防和运维等多个方面的应用。例如,在智慧教学领域,通过多媒体教学、智慧录播、电子图书馆等技术,实现教学资源的共享和教学模式的创新。在智慧教务方面,校园网络、考场监控、智慧班牌等系统为校园管理提供了便捷和高效。智慧安防系统包括视频监控、一键报警、阳光厨房等,确保校园安全。智慧运维则通过综合管理平台、设备管理、能效管理和资产管理,实现校园设施的智能化管理。 智慧校园的优势和价值体现在个性化互动的智慧教学、协同高效的校园管理、无处不在的校园学习、全面感知的校园环境和轻松便捷的校园生活等方面。通过智慧校园的建设,可以促进教育资源的均衡化,提高教育质量和管理效率,同时保障校园安全和提升师生的学习体验。 总之,智慧校园解决方案通过整合现代信息技术,如云计算、大数据、物联网和人工智能,为教育行业带来了革命性的变革。它不仅提高了教育的质量和效率,还为师生创造了一个更加安全、便捷和富有智慧的学习与生活环境。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值