本文主要是介绍poj 3348 Cows (叉积计算凸包面积),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意挺简单的,求凸包的面积然后除于50即可。
面积:固定某一点,找枚举凸包中的点,用叉积求即可:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <stack>
#include <algorithm>
#define PI acos(-1)
#define MAXN 1000+10
using namespace std;int N,L;struct point{int x,y;
}p[MAXN];
int cross(int x1,int y1,int x2,int y2){return x2*y1 - x1*y2;
}
double dis(const point &a,const point &b){return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
double dis(int x1,int y1,int x2,int y2){return sqrt((x1 - x2)*(x1 - x2) + (y1 - y2) * (y1 - y2));
}
bool cmp(const point &a,const point &b){int x1 = a.x - p[0].x,y1 = a.y - p[0].y;int x2 = b.x - p[0].x,y2 = b.y - p[0].y;if(cross(x1,y1,x2,y2) != 0)return cross(x1,y1,x2,y2) < 0;elsereturn dis(a,p[0]) < dis(b,p[0]);
}
bool ok(point p1,point p2,point p3){int x1 = p2.x - p1.x;int y1 = p2.y - p1.y;int x2 = p3.x - p2.x;int y2 = p3.y - p2.y;int c = cross(x1,y1,x2,y2);if(c <= 0) return true;else return false;
}
//凸包扫描算法
void GrahamScan(){stack<point> s;s.push(p[0]);s.push(p[1]);int i = 2;while(i < N ){point p2 = s.top();s.pop();point p1 = s.top();point p3 = p[i];if(ok(p1,p2,p3)){s.push(p2);s.push(p[i]);i++;}}s.pop();double S = 0.0;point pfa = p[N-1];point p1 = s.top();s.pop();while(!s.empty()){point p2 = s.top();s.pop();S += abs(cross(p1.x - pfa.x,p1.y - pfa.y,p2.x - pfa.x,p2.y-pfa.y)/2.0);p1 = p2;}printf("%d\n",(int)(S/50));
}int main()
{while(~scanf("%d",&N)){int m = 1;scanf("%d%d",&p[0].x,&p[0].y);for(int i=1;i<N;i++){point pt;scanf("%d%d",&pt.x,&pt.y);if(pt.y < p[0].y || (pt.y == p[0].y && pt.x < p[0].x)){p[m].x = p[0].x;p[m++].y = p[0].y;p[0].x = pt.x;p[0].y = pt.y;}else{p[m].x = pt.x;p[m++].y = pt.y;}}sort(p+1,p+N,cmp);GrahamScan();}return 0;
}
这篇关于poj 3348 Cows (叉积计算凸包面积)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!