hdu 5258 数长方形-2015年百度之星程序设计大赛 - 复赛

数长方形

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


Problem Description
小度熊喜欢玩木棒。一天他在玩木棒的时候,发现一些木棒会形成长方形。小度熊可能是处女座吧,他只会将木棒横竖摆放,这样会形成很多长方形。现在给你一些横竖摆放的木棒,请你帮小度熊数一数形成了多少个长方形。

为了简化题目,一个木棒的端点不会在另一个木棒上,也就是说,木棒的端点不会在长方形上。
 

Input
第一行一个整数T,表示T组数据,不超过100组。

每组数据中,第一行是n,代表有多少个木棒,n不会超过25。接下来n行,每行4个整数 x1,y1,x2,y2 ,代表木棒的坐标,绝对值不超过1000。

所有的木棒都是横竖摆放的,也就是说 x1=x2 或者 y1=y2 ,没有长为0的木棒。
 

Output
对于每组测试数据,先输出一行

Case #i:

然后输出一个整数,代表有多少个长方形。
 

Sample Input
  
  
2 4 3 0 3 3 4 0 4 3 2 1 5 1 2 2 5 2 4 3 0 3 3 4 0 4 3 2 1 5 1 2 2 -5 2
 

Sample Output
  
  
Case #1: 1 Case #2: 0
 

Source

2015年百度之星程序设计大赛 - 复赛


思路:

这题没啥好说的,数据量这么小,暴力判断就行了。PS,最好分一下组,然后排个序,逻辑上会更清晰一些!

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

#define max(x, y) ((x) > (y) ? (x) : (y))
#define min(x, y) ((x) < (y) ? (x) : (y))

struct Stick
{
    int x1, y1;
    int x2, y2;

    Stick(int a, int b, int c, int d)
    {
        x1 = a, y1 = b, x2 = c, y2 = d;
        if (x1 > x2)
        {
            swap(x1, x2);
            swap(y1, y2);
        }
        else if (y1 > y2)
        {
            swap(x1, x2);
            swap(y1, y2);
        }
    }

    bool operator < (Stick &obj)
    {
        if (x1 == x2)
        {
            // 竖着的
            return x1 < obj.x1;
        }
        else if (y1 == y2)
        {
            // 横着的
            return y1 < obj.y1;
        }

        return false;
    }
};

int n;
vector<Stick> vecHor;
vector<Stick> vecVer;


void Solve()
{
    int ans = 0;

    sort(vecHor.begin(), vecHor.end());
    sort(vecVer.begin(), vecVer.end());

    for (int i = 0; i < vecHor.size(); ++i)
    {
        for (int j = i + 1; j < vecHor.size(); ++j)
        {
            if (vecHor[i].y1 == vecHor[j].y1)
            {
                continue;
            }

            for (int x = 0; x < vecVer.size(); ++x)
            {
                for (int y = x + 1; y < vecVer.size(); ++y)
                {
                    if (vecVer[x].x1 == vecVer[y].x1)
                    {
                        continue;
                    }

                    if (max(vecHor[i].x1, vecHor[j].x1) <= vecVer[x].x1 &&
                        vecVer[y].x1 <= min(vecHor[i].x2, vecHor[j].x2) &&
                        max(vecVer[x].y1, vecVer[y].y1) <= vecHor[i].y1 &&
                        vecHor[j].y1 <= min(vecVer[x].y2, vecVer[y].y2))
                    {
                        ++ans;
                    }
                }
            }
        }
    }

    printf("%d\n", ans);
}

int main()
{
    //freopen("input.txt", "r", stdin);

    int times;
    scanf("%d", ×);

    for (int i = 1; i <= times; ++i)
    {
        vecHor.clear();
        vecVer.clear();

        scanf("%d", &n);
        int x1, y1, x2, y2;
        for (int j = 0; j < n; ++j)
        {
            scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
            Stick stick(x1, y1, x2, y2);
            if (y1 == y2)
            {
                vecHor.push_back(stick);
            }
            else if (x1 == x2)
            {
                vecVer.push_back(stick);
            }
        }

        printf("Case #%d:\n", i);
        Solve();
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值