HDU 4709 Herding(叉乘法)

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.
 

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
  
  
1 4 -1.00 0.00 0.00 -3.00 2.00 0.00 2.00 2.00
 

Sample Output
  
  
2.00
 

Source
 

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
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值