POJ3348 Cows【凸包+多边形求面积】
?POJ3348Cows 凸包+多邊形求面積?
個人分類:?計算幾何凸包
| Language:?Default Cows
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 forced to save money on buying fence posts by using trees as fence posts wherever possible. Given the locations of some trees, you are to help farmers try to create the largest pasture that is possible. Not all the trees will need to be used. However, because you will oversee the construction of the pasture yourself, all the farmers want to know is how many cows they can put in the pasture. It is well known that a cow needs at least 50 square metres of pasture to survive. Input The first line of input contains a single integer,?n?(1 ≤?n?≤ 10000), containing the number of trees that grow on the available land. The next?n?lines contain the integer coordinates of each tree given as two integers?x?and?yseparated by one space (where -1000 ≤ x, y ≤ 1000). The integer coordinates correlate exactly to distance in metres (e.g., the distance between coordinate (10; 11) and (11; 11) is one metre). Output You are to output a single integer value, the number of cows that can survive on the largest field you can construct using the available trees. Sample Input 4 0 0 0 101 75 0 75 101Sample Output 151 |
題意:給出一些點,圈出一個最大面積,每50平方養一頭牛,問最多能養多少牛
求凸包 + (int)求面積 / 50
//POJ--1228 #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #define eps 1e-8 using namespace std;struct point {double x,y; }; point p[1010],stack[1010]; int N,top; //叉積 double multi(point p1, point p2, point p3) {return (p2.x - p1.x) * (p3.y - p1.y) - (p2.y - p1.y) * (p3.x - p1.x); } //距離公式 double dis(point a, point b) {return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y)); } //極角排序比較器 bool cmp(point c, point d) {double k = multi(p[0], c, d);if(k>0) return true;if(k<0) return false;return dis(c,p[0])<dis(d,p[0]); } //求凸包 void Convex() {//第一個點p[0]為左下角的點for(int i = 1; i < N; i++){point temp;if(p[i].y < p[0].y || ( p[i].y == p[0].y && p[i].x < p[0].x)){temp = p[i];p[i] = p[0];p[0] = temp;}}sort(p + 1, p+N , cmp);//不包括第一個點stack[0] = p[0];stack[1] = p[1];top = 1;for(int i = 2; i < N; i++){while(top >= 1 && multi(stack[top - 1], stack[top], p[i]) < 0) top--;//共線的點也壓入凸包內;top++;stack[top] = p[i];} } //判斷每條邊是否有至少三個點; bool judge() {for(int i=1;i<top;i++){if((multi(stack[i-1],stack[i+1],stack[i]))!=0&&(multi(stack[i],stack[i+2],stack[i+1]))!=0)return false;}return true; } double area() {double res=0;for(int i=1;i<top;i++){res+=(multi(stack[0],stack[i],stack[i+1])/2.0);}return res; } int main() {while(scanf("%d",&N)!=EOF){for(int i=0;i<N;i++)scanf("%lf%lf",&p[i].x,&p[i].y);Convex();int num=(int)floor(area()/50.0);printf("%d\n",num);}return 0; }?
總結
以上是生活随笔為你收集整理的POJ3348 Cows【凸包+多边形求面积】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: POJ 1584 A Round Peg
- 下一篇: C++结构体多级排序的三种方法