zoukankan      html  css  js  c++  java
  • 数长方形有多少个?POJ(1693)

    题目链接:http://poj.org/problem?id=1693

    解题报告:

    随机选两根横的,再找一下与这两根横线相交的竖线有多少根,m,那么就有(m-1)*m/2个长方形。

    #include <iostream>
    #include <stdio.h>
    #include <algorithm>
    #include <string.h>
    
    using namespace std;
    
    const int maxn = 100;
    
    struct line {
        int x[2];
        int y[2];
    }row[maxn],col[maxn];
    
    int n;
    
    int main()
    {
        int Case;
        cin>>Case;
        int r,c;
        while(Case--)
        {
            r=c=0;
            int ans=0;
            cin>>n;
            for(int i=0;i<n;i++)
            {
                int x1,y1,x2,y2;
                scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    
                if(y1==y2)  ///水平
                {
                    row[r].x[0]=min(x1,x2);
                    row[r].x[1]=max(x1,x2);
    
                    row[r].y[0]=y1;
                    row[r++].y[1]=y2;
                }
                else {
                    ///垂直
                    col[c].y[0]=min(y1,y2);
                    col[c].y[1]=max(y1,y2);
    
                    col[c].x[0]=col[c].x[1]=x1;
                    c++;
                }
            }
    
            int maxr,minr,maxc,minc;
    
            int tmp;
            for(int i=0;i<r-1;i++)
            {
                for(int j=i+1;j<r;j++)
                {
                    if(row[i].x[1]<row[j].x[0]||row[i].y[0]==row[j].y[0])
                        continue;
    
                    tmp=0;
                    minr=max(row[i].x[0],row[j].x[0]);
                    maxr=min(row[i].x[1],row[j].x[1]);
    
                    maxc=max(row[i].y[0],row[j].y[0]);
                    minc=min(row[i].y[0],row[j].y[0]);
    
                    for(int k=0;k<c;k++)
                    {
                        if(col[k].x[0]>=minr&&col[k].x[1]<=maxr&&col[k].y[0]<=minc&&col[k].y[1]>=maxc)
                            tmp++;
                    }
                    ans+=(tmp-1)*tmp/2;
                }
            }
            printf("%d
    ",ans);
        }
    }
  • 相关阅读:
    oracle-sql脚本
    vue生命周期
    使用vue搭建项目(创建手脚架)
    bootcss
    miniMobile(手机)
    mui(手机)
    layui
    Element
    如何学好Spring
    使用Vue做评论+localStorage存储(js模块化)
  • 原文地址:https://www.cnblogs.com/TreeDream/p/5601562.html
Copyright © 2011-2022 走看看