HDU 4709 Herding(叉乘法)
来源:互联网 发布:Servlet接收json gson 编辑:程序博客网 时间:2024/06/05 03:29
Herding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2194 Accepted Submission(s): 629
Problem Description
Little John is herding his father's cattles. As a lazy boy, he cannot tolerate chasing the cattles all the time to avoid unnecessary omission. Luckily, he notice that there were N trees in the meadow numbered from 1 to N, and calculated their cartesian coordinates (Xi, Yi). To herding his cattles safely, the easiest way is to connect some of the trees (with different numbers, of course) with fences, and the close region they formed would be herding area. Little John wants the area of this region to be as small as possible, and it could not be zero, of course.
Input
The first line contains the number of test cases T( T<=25 ). Following lines are the scenarios of each test case.
The first line of each test case contains one integer N( 1<=N<=100 ). The following N lines describe the coordinates of the trees. Each of these lines will contain two float numbers Xi and Yi( -1000<=Xi, Yi<=1000 ) representing the coordinates of the corresponding tree. The coordinates of the trees will not coincide with each other.
The first line of each test case contains one integer N( 1<=N<=100 ). The following N lines describe the coordinates of the trees. Each of these lines will contain two float numbers Xi and Yi( -1000<=Xi, Yi<=1000 ) representing the coordinates of the corresponding tree. The coordinates of the trees will not coincide with each other.
Output
For each test case, please output one number rounded to 2 digits after the decimal point representing the area of the smallest region. Or output "Impossible"(without quotations), if it do not exists such a region.
Sample Input
14-1.00 0.000.00 -3.002.00 0.002.00 2.00
Sample Output
2.00
Source
2013 ACM/ICPC Asia Regional Online —— Warmup
Recommend
liuyiding
题目意思是,给出n个点的横纵坐标,要求任意多个点围成的闭合区域的最小面积S(S>0).
分析:要使任意多个点围成的闭合区域面积最小,围成的必然是三角形。
由于数据不大,直接(n3)可以过。一开始想到得求面积的公式为海伦公式,交了多次一直WA,至今不知道原因,还望大神指点:
//Result :Wrong answer
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <string>#include <vector>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <set>#include <map>using namespace std;typedef long long LL;const int mx = 1000 + 5;const double eps=1e-10;const double inf = 1e9;struct point { double x,y;} P[105];double slove(int a,int b) { double s=(P[b].x-P[a].x)*(P[b].x-P[a].x)+(P[b].y-P[a].y)*(P[b].y-P[a].y); return sqrt(s);}double getS(int i,int j,int k) { double a=slove(i,j); double b=slove(j,k); double c=slove(i,k); double p=(a+b+c)/2.0; return sqrt(p*(p-a)*(p-b)*(p-c));}int main() { int T,N; scanf("%d",&T); while(T--) { scanf("%d",&N); bool flag = 0; double minS=inf; for(int i=1; i<=N; i++) scanf("%lf %lf",&P[i].x,&P[i].y); for(int i=1; i<=N; i++) for(int j=i+1; j<=N; j++) for(int k=j+1; k<=N; k++) { double t = getS(i,j,k); if(fabs(t)>eps){ minS = t < minS ? t : minS; flag = true; } } if(flag) printf("%.2lf\n",minS); else printf("Impossible\n"); } return 0;}
之后改用叉乘法求面积,AC了,代码如下:
#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <string>#include <vector>#include <cmath>#include <ctime>#include <queue>#include <stack>#include <set>#include <map>using namespace std;typedef long long LL;const int mx = 1000 + 5;const double eps=1e-10;const double inf = 1e9;struct point { double x,y;} P[105];double getS(point a,point b,point c) { //叉积求面积 return fabs((b.x - a.x) * (c.y - a.y) - (b.y - a.y)*(c.x - a.x))/2;}int main() { int T,N; scanf("%d",&T); while(T--) { scanf("%d",&N); bool flag = 0; double minS=inf; for(int i=1; i<=N; i++) scanf("%lf %lf",&P[i].x,&P[i].y); for(int i=1; i<=N; i++) for(int j=i+1; j<=N; j++) for(int k=j+1; k<=N; k++) { double t = getS(P[i],P[j],P[k]); if(fabs(t)>eps){ minS = t < minS ? t : minS; flag = true; } } if(flag) printf("%.2lf\n",minS); else printf("Impossible\n"); } return 0;}
顺便附上叉乘法求面积的通用方法:
/*叉乘法求任意多边形面积用叉乘的方法求任意多边形的面积, 很实用的算法,代码很简洁语法:result=polygonarea(Point *polygon,intN);参数:*polygon:多变形顶点数组N:多边形顶点数目返回值:多边形面积注意:支持任意多边形,凹、凸皆可多边形顶点输入时按顺时针顺序排列*/struct node { double x,y;};double slove(node *adge,int N) { double S = 0; for(int i=0; i<N; i++) { int j = (i + 1) % N; S += adge[i].x * adge[j].y; S -= adge[i].y * adge[j].x; } S /= 2; return(S < 0 ? -S : S);}
1 0
- HDU 4709 Herding(叉乘法)
- HDU 4709 Herding(热身赛第四题)
- HDU 4709 Herding(求三角形面积)
- HDU 4709 Herding(叉积求三角形面积)
- hdu 4709 Herding
- hdu 4709 Herding acm
- hdu - 4709 - Herding
- HDU 4709 Herding
- hdu 4709 Herding
- HDU 4709 Herding
- HDU 4709 Herding
- HDU - 4709 Herding
- HDU 4709 Herding
- hdu 4709 Herding (数学)
- HDU 4709 Herding
- HDU 4709 Herding
- HDU 4709 Herding
- hdu 4709 Herding
- 微信恢复工具 MMRecovery 犹如一朵青莲
- 深度学习-theano-windows -cuda-环境搭建
- 浅谈Java中的hashcode方法
- HDU 1036.Average is not Fast Enough!【精度及格式控制】【8月23】
- 例题9-8 颜色的长度 UVa1625
- HDU 4709 Herding(叉乘法)
- 【C++数据结构】顺序表的模版类实现
- setup subline text3
- JS的HTML DOM disabled 属性
- 1-2、自定义Partitioner代码
- 虚拟机与docker
- 【ACM】华为oj--合法ip
- iOS项目开发实战——学会使用TableView列表控件(一)
- 在Word中用快捷键转换英文字母大小写