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\n"); 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\n",ans);
            else printf("Impossible\n");
}
int main()
{
    ios::sync_with_stdio(0);
    int t;
    scanf("%d",&t);
    while(t--)
    solve();
    return 0;
}
复制代码

 

posted @   JL_Zhou  阅读(355)  评论(0编辑  收藏  举报
编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示