Triangles(模拟+枚举)

时间限制: 1 Sec 内存限制: 128 MB
[提交] [状态]
题目描述
Farmer John would like to create a triangular pasture for his cows.
There are N fence posts (3≤N≤100) at distinct points (X1,Y1)…(XN,YN) on the 2D map of his farm. He can choose three of them to form the vertices of the triangular pasture as long as one of the sides of the triangle is parallel to the x-axis and another side is parallel to the y-axis.

What is the maximum area of a pasture that Farmer John can form? It is guaranteed that at least one valid triangular pasture exists.
输入
The first line of the input contains the integer N. Each of the next N lines contains two integers Xi and Yi, each in the range −10^ 4…10^ 4 inclusive, describing the location of a fence post.
输出
As the area itself is not necessarily an integer, output two times the maximum area of a valid triangle formed by the fence posts.
样例输入 Copy
4
0 0
0 1
1 0
1 2
样例输出 Copy
2
提示
Posts at (0,0), (1,0), and (1,2) form a triangle of area 1. Thus, the answer is 2⋅1=2. There is only one other triangle, with area 0.5.
题目大意是在给定的若干个点中寻找满足一条边平行于x轴,另一条边平行于y轴的三角形,并求在所有满足这种条件的三角形的面积的二倍的最大值。
一开始我采用寻找一对相互垂直的边,结果WA,才知道没有考虑到不一定与坐标轴平行。
后来一想就是暴力枚举所有的情况。
首先对所有坐标排序,横坐标小的在前,横坐标相同时纵坐标小的在前。然后暴力枚举所有情况:

所有可能的情况,其中i,j,k分别代表3个不同的点,先保证两个点的连线平行于x(y)轴,然后保证其中一个点与第三个点的连线平行于y(x)轴

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct p
{
    int x;
    int y;
};
struct p point[105];
int main()
{
    int n;
    int i;
    int j;
    int k;
    int ans=0;
    scanf("%d",&n);
    for(i=0;i<=n-1;i++)
    {
        scanf("%d %d",&point[i].x,&point[i].y);
    }
    for(i=0;i<=n-2;i++)
    {
        for(j=i+1;j<=n-1;j++)
        {
            if(point[i].x>point[j].x)swap(point[i],point[j]);
            else if(point[i].x==point[j].x)
            {
                if(point[i].y>point[j].y)swap(point[i],point[j]);
            }
        }
    }
    for(i=0;i<=n-3;i++)
    {
        for(j=i+1;j<=n-2;j++)
        {
            for(k=j+1;k<=n-1;k++)
            {
                if(point[i].x==point[j].x)
                {
                    if(point[i].y==point[k].y)ans=max(ans,abs(point[i].y-point[j].y)*abs(point[i].x-point[k].x));
                    else if(point[j].y==point[k].y)ans=max(ans,abs(point[i].y-point[j].y)*abs(point[j].x-point[k].x));
                }
                else if(point[i].x==point[k].x)
                {
                    if(point[i].y==point[j].y)ans=max(ans,abs(point[i].y-point[k].y)*abs(point[i].x-point[j].x));
                    else if(point[j].y==point[k].y)ans=max(ans,abs(point[k].y-point[i].y)*abs(point[j].x-point[k].x));
                }
                else if(point[k].x==point[j].x)
                {
                    if(point[i].y==point[j].y)ans=max(ans,abs(point[k].y-point[j].y)*abs(point[i].x-point[j].x));
                    else if(point[i].y==point[k].y)ans=max(ans,abs(point[k].y-point[j].y)*abs(point[i].x-point[k].x));
                }
                else if(point[i].y==point[j].y)
                {
                    if(point[i].x==point[k].x)ans=max(ans,abs(point[i].x-point[j].x)*abs(point[i].y-point[k].y));
                    else if(point[j].x==point[k].x)ans=max(ans,abs(point[i].x-point[j].x)*abs(point[j].y-point[k].y));
                }
                else if(point[j].y==point[k].y)
                {
                    if(point[i].x==point[k].x)ans=max(ans,abs(point[k].x-point[j].x)*abs(point[i].y-point[k].y));
                    else if(point[j].x==point[i].x)ans=max(ans,abs(point[k].x-point[j].x)*abs(point[j].y-point[i].y));
                }
                else if(point[i].y==point[k].y)
                {
                    if(point[i].x==point[j].x)ans=max(ans,abs(point[i].x-point[k].x)*abs(point[i].y-point[j].y));
                    else if(point[j].x==point[k].x)ans=max(ans,abs(point[i].x-point[k].x)*abs(point[j].y-point[k].y));
                }
            }
        }
    }
    printf("%d",ans);
    return 0;
}

这个方法可能比较麻烦,如果有简便方法欢迎在评论中分享!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值