POJ 3348 Cows(凸包+多边形面积)

先求出凸包,然后利用凸包求出面积,除以50就是答案

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;

const int MAXN=10005;

struct Point {
    double x, y;
    Point() {}
    Point(double x, double y) {
        this->x = x;
        this->y = y;
    }
    void read() {
        scanf("%lf%lf", &x, &y);
    }
} list[MAXN], p[MAXN];

typedef Point Vector;

Vector operator + (Vector A, Vector B) {
    return Vector(A.x + B.x, A.y + B.y);
}

Vector operator - (Vector A, Vector B) {
    return Vector(A.x - B.x, A.y - B.y);
}

Vector operator * (Vector A, double p) {
    return Vector(A.x * p, A.y * p);
}

Vector operator / (Vector A, double p) {
    return Vector(A.x / p, A.y / p);
}

double Cross(Vector A, Vector B) {return A.x * B.y - A.y * B.x;} //叉积

int stack[MAXN], top;

int xmult(Point p0, Point p1, Point p2) {
    return (p1.x - p0.x) * (p2.y - p0.y) - (p1.y - p0.y) * (p2.x - p0.x);
}

double dist(Point p1,Point p2) {
    return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}

//极角排序函数 , 角度相同则距离小的在前面
bool cmp(Point p1,Point p2)  {
    int tmp= xmult(list[0], p1, p2);
    if(tmp > 0) return true;
    else if(tmp == 0 && dist(list[0], p1) < dist(list[0], p2)) return true;
    else return false;
}

//输入并把最左下方的点放在list[0]并且进行极角排序
void init(int n)  {
    int i, k;
    Point p0;
    scanf("%lf%lf", &list[0].x, &list[0].y);
    p0.x = list[0].x;
    p0.y = list[0].y;
    k = 0;
    for(i = 1; i < n; i++) {
        scanf("%lf%lf", &list[i].x, &list[i].y);
        if((p0.y > list[i].y) || ((p0.y == list[i].y) && (p0.x > list[i].x))) {
            p0.x = list[i].x;
            p0.y = list[i].y;
            k = i;
        }
    }
    list[k] = list[0];
    list[0] = p0;
    sort(list + 1, list + n, cmp);
}

void graham(int n) {
    int i;
    if(n == 1) {top = 0; stack[0] = 0;}
    if(n == 2) {
        top = 1;
        stack[0] = 0;
        stack[1] = 1;
    }
    if(n > 2) {
        for(i = 0; i <= 1; i++) stack[i] = i;
        top = 1;
        for(i = 2; i < n; i++) {
            while(top > 0 && xmult(list[stack[top - 1]], list[stack[top]], list[i]) <= 0) top--;
            top++;
            stack[top]=i;
        }
    }
    for (int i = 0; i <= top; i++)
        p[i] = list[stack[i]];
}

double PolygonArea(Point *p, int n) {
    double area = 0;
    for (int i = 1; i < n - 1; i++)
        area += Cross(p[i] - p[0], p[i + 1] - p[0]);
    return fabs(area) / 2;
}

int n;

int main() {
    while (~scanf("%d", &n)) {
        init(n);
        graham(n);
        printf("%d\n", (int)(PolygonArea(p, top +1) / 50.0));
    }
    return 0;
}
时间: 2024-11-06 08:37:13

POJ 3348 Cows(凸包+多边形面积)的相关文章

POJ 3348 Cows 凸包 求面积

LINK 题意:给出点集,求凸包的面积 思路:主要是求面积的考察,固定一个点顺序枚举两个点叉积求三角形面积和除2即可 /** @Date : 2017-07-19 16:07:11 * @FileName: POJ 3348 凸包面积 叉积.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <std

poj 3348 Cows 凸包 求多边形面积 计算几何 难度:0 Source:CCC207

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7038   Accepted: 3242 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

POJ 3348 Cows(凸包面积)

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7515   Accepted: 3418 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

POJ 3348 Cows [凸包 面积]

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9022   Accepted: 3992 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

poj 3348 Cow 凸包面积

Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8122   Accepted: 3674 Description Your friend to the south is interested in building fences and turning plowshares into swords. In order to help with his overseas adventure, they are f

poj 3348 Cows 求凸包面积

题目链接 大意: 求凸包的面积. #include <iostream> #include <vector> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <map> #include <set> #include <string> #include <queue>

●POJ 3348 Cows

题链: http://poj.org/problem?id=3348 题解: 计算几何,凸包,多边形面积 好吧,就是个裸题,没什么可讲的. 代码: #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define MAXN 10050 using namespace std; const double eps=1e-8

简单几何(向量旋转+凸包+多边形面积) UVA 10652 Board Wrapping

题目传送门 题意:告诉若干个矩形的信息,问他们在凸多边形中所占的面积比例 分析:训练指南P272,矩形面积长*宽,只要计算出所有的点,用凸包后再求多边形面积.已知矩形的中心,向量在原点参考点再旋转,角度要转换成弧度制. /************************************************ * Author :Running_Time * Created Time :2015/11/10 星期二 10:34:43 * File Name :UVA_10652.cpp

Area - POJ 1654(求多边形面积)

题目大意:从原点开始,1-4分别代表,向右下走,向右走,向右上走,向下走,5代表回到原点,6-9代表,向上走,向左下走,向左走,向左上走.求出最后的多边形面积. 分析:这个多边形面积很明显是不规则的,可以使用向量积直接求出来面积即可. 代码如下: ------------------------------------------------------------------------------------------------------------------------------