首页 > ACM题库 > HDU-杭电 > HDU 4709-Herding-计算几何-[解题报告]HOJ
2015
09-17

HDU 4709-Herding-计算几何-[解题报告]HOJ

Herding

问题描述 :

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.

输入:

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 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.

样例输入:

1
4
-1.00 0.00
0.00 -3.00
2.00 0.00
2.00 2.00

样例输出:

2.00

PS:枚举三角形三个点求面积的时候,要保证三角形的面积不为零,注意面积为负的情况。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

struct point {
    double x, y;
    point(double x=0, double y=0):x(x),y(y) {}
};
point operator - (point A, point B) {
    return point(A.x-B.x, A.y-B.y);
}
double Cross(point A, point B) {
    return A.x*B.y - A.y*B.x;
}
int n;
vector<point> v;
double area(point a, point b, point c) {
    double mid = Cross(b-a, c-a);
    mid = mid/2.0;
    return fabs(mid);
}
double work() {
    int len = v.size();
    double ans = 999999999;
    double tmp = ans;
    for(int i = 0; i < len; i++) {
        for(int j = i+1; j < len; j++) {
            for(int k = j+1; k < len; k++) {
                if(i!=j && i!=k && j!=k) {
                    double mid = area(v[i], v[j], v[k]);
                    if(mid > 0) {   // WA.
                        ans = min(ans, mid);
                    }
                }
            }
        }
    }
    if(ans==tmp) return 0;
    else return ans;
}

int main()
{
    int T;
    scanf("%d", &T);
    point t;
    while(T--) {
        scanf("%d", &n);
        v.clear();
        for(int i = 0; i < n; i++) {
            scanf("%lf%lf", &t.x, &t.y);
            v.push_back(t);
        }
        double res = work();
        if(res+0.005<0.01) printf("Impossible\n");
        else printf("%.2lf\n", res);
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/achiberx/article/details/24529381