[ZOJ1426 ][POJ1693] Counting Rectangles

本文介绍了一种通过水平和竖直线段输入来计算所形成矩形数量的算法。该算法利用结构体存储线段信息,并通过四重循环检查每一对线段是否相交来找出所有可能构成矩形的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ZOJ: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=426

POJ: http://poj.org/problem?id=1693


题目大意:

给出一些水平和竖直线段,问围城多少矩形。

注意简化条件:

 The input line segments are such that each intersection point comes from the intersection of exactly one horizontal segment and one vertical segment.


解题思路:

据说可以暴力,就暴力了……很久没有1AC了……ZOJ交完交POJ交错题了……


源代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 110 

typedef struct line_ver
{
    int x, y1, y2;
}line_var;

typedef struct line_hor
{
    int y, x1, x2;
}line_hor;

int v,h,n;
line_ver linev[maxn];
line_hor lineh[maxn];

int min(int x, int y){if (x>y) return y; return x;}
int max(int x, int y){if (x>y) return x; return y;}

void init()
{
    int x1,y1,x2,y2;
    scanf("%d", &n);
    v=h=0;
    for (int i=0; i<n; i++)
    {
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        if (x1==x2)     //vertical
        {
            linev[v].x=x1; linev[v].y1=min(y1,y2); linev[v++].y2=max(y1,y2);
        }
        else            //horizontal
        {
            lineh[h].y=y1; lineh[h].x1=min(x1,x2); lineh[h++].x2=max(x1,x2);
        }
    }
}

bool intersect(int vv, int hh)
{
    if (linev[vv].x>=lineh[hh].x1 && linev[vv].x<=lineh[hh].x2)
        if (lineh[hh].y>=linev[vv].y1 && lineh[hh].y<=linev[vv].y2)
            return true;
    return false;
}

int solve()
{
    int ret=0;
    for (int v1=0; v1<v-1; v1++)
        for (int h1=0; h1<h-1; h1++)
            if (intersect(v1, h1))
                for (int v2=v1+1; v2<v; v2++)
                    if (intersect(v2, h1))
                        for (int h2=h1+1; h2<h; h2++)
                            if (intersect(v1, h2) && intersect(v2, h2))
                                ret++;
    return ret;                            
}

int main()
{
    int cs, ans;
    scanf("%d", &cs);
    while (cs--)
    {
        init();
        ans=solve();
        printf("%d\n", ans);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值