[ACM]hdu Herding(枚举+三角形面积)

Herding

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 5   Accepted Submission(s) : 2

Font: Times New Roman | Verdana | Georgia

Font Size:

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

2013 ACM/ICPC Asia Regional Online —— Warmup

 

解题思路:

本题就是给定几个点,然后从中选择几个点围成一定的面积,要求这个最小的面积,一开始想复杂了,以为四个或五个的点也能组成最小面积,其实最小的是三个点,因为四个或五个点其中里面选三个点肯定面积要小,所以本题的本质也就是求最小三角形的面积。

代码:

 

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
struct point
{
    double x,y;
}p[103];//构造点

double area(point p1,point p2,point p3)
{
    return abs(p1.x*p2.y+p1.y*p3.x+p2.x*p3.y-p2.y*p3.x-p1.x*p3.y-p1.y*p2.x)/2.0;
}//计算三角形的面积

int main()
{
    int t,n;
    cin>>t;
    for(int ni=1;ni<=t;ni++)
    {
        cin>>n;
        for(int i=1;i<=n;i++)
            cin>>p[i].x>>p[i].y;
        int flag=0;
        double Min=9999999;//用于最后输出
        for(int i=1;i<=n;i++)//枚举三个不同的点,三重循环完成
        {
            for(int j=i+1;j<=n;j++)
            {
                for(int k=j+1;k<=n;k++)
                {
                    double temp=area(p[i],p[j],p[k]);

                        if(temp<Min&&temp!=0)//temp不为0才能构成三角形
                        {
                            Min=temp;
                            flag=1;
                        }
                }
            }
        }
        if(flag)
            cout<<setiosflags(ios::fixed)<<setprecision(2)<<Min<<endl;
        else
            cout<<"Impossible"<<endl;
    }
    return 0;
}


要点:给定三个点,求其面积

double area(point p1,point p2,point p3)
{
    return abs(p1.x*p2.y+p1.y*p3.x+p2.x*p3.y-p2.y*p3.x-p1.x*p3.y-p1.y*p2.x)/2.0;
}

即求行列式

1    1     1

x1  x2   x3

y1  y2   y3

posted @ 2014-02-24 14:19  同学少年  阅读(235)  评论(0编辑  收藏  举报