Herding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1902    Accepted Submission(s): 540


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
 思路:在n个点里面找出3个点,使三角形的面积最小
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <stack>
#include <cstring>
#include <cmath>
#include <iostream>
#define INF 1 << 25
#define eps 1e-8
using namespace std;
struct node{
    double x,y;
}a[120];
int n;
int check(node a,node b,node c)//向量判断是否3点在同一直线
{
    node v1, v2;
    v1.x = a.x - b.x;
    v1.y = a.y - b.y;
    v2.x = c.x - b.x;
    v2.y = c.y - b.y;
    if(fabs(v1.x * v2.y - v2.x * v1.y) < eps ) return 0;
    else return 1;
}
double calc(node a,node b,node c)//求面积
{
    double res = 0;
    res = fabs(a.x * b.y + b.x * c.y + c.x * a.y - b.x * a.y - c.x * b.y - a.x * c.y);
    return res/2.0;
}
void solve()
{
    scanf("%d",&n);
    for(int i = 1; i <=n ; ++i)
    scanf("%lf%lf",&a[i].x,&a[i].y);
    if(n == 1 || n == 2) { printf("Impossible
"); return; }
    double ans = INF;
    for(int i = 1; i <= n ;++i)//暴力枚举
        for(int j = i + 1; j <=n ; ++j)
            for(int k = j + 1; k <= n ; ++k)
            if(check(a[i],a[j],a[k])){
                double now = calc(a[i],a[j],a[k]);
                if(now < ans) ans = now;
            }
           // cout << ans <<endl;
            if(ans != INF)
            printf("%.2f
",ans);
            else printf("Impossible
");
}
int main()
{
    ios::sync_with_stdio(0);
    int t;
    scanf("%d",&t);
    while(t--)
    solve();
    return 0;
}
原文地址:https://www.cnblogs.com/orchidzjl/p/4439955.html