[ZOJ1426 ][POJ1693] Counting Rectangles

来源:互联网 发布:淘宝直通车关键词 编辑:程序博客网 时间:2024/04/27 20:21

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);    }}


原创粉丝点击