zoukankan      html  css  js  c++  java
  • hihoCoder1040 矩形判断

    #1040 : 矩形判断

    时间限制:1000ms
    单点时限:1000ms
    内存限制:256MB

    描述

    给出平面上4条线段,判断这4条线段是否恰好围成一个面积大于0的矩形。

    输入

    输入第一行是一个整数T(1<=T<=100),代表测试数据的数量。

    每组数据包含4行,每行包含4个整数x1, y1, x2, y2 (0 <= x1, y1, x2, y2 <= 100000);其中(x1, y1), (x2,y2)代表一条线段的两个端点。

    输出

    每组数据输出一行YES或者NO,表示输入的4条线段是否恰好围成矩形。

    样例输入
    3
    0 0 0 1
    1 0 1 1
    0 1 1 1
    1 0 0 0
    0 1 2 3
    1 0 3 2
    3 2 2 3
    1 0 0 1
    0 1 1 0
    1 0 2 0
    2 0 1 1
    1 1 0 1
    
    样例输出
    YES
    YES
    NO
    
    分析:先判断四条边是否首尾相连,即是否构成四边形,再判断四边形是否是矩形。
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    struct Node{
        int x1,y1,x2,y2;
    }a[10];
    
    int f(Node A,Node B)
    {
        int ans=0;
        if(A.x1==B.x1&&A.y1==B.y1) ans+=1;
        if(A.x1==B.x2&&A.y1==B.y2) ans+=10;
        if(A.x2==B.x1&&A.y2==B.y1) ans+=100;
        if(A.x2==B.x2&&A.y2==B.y2) ans+=1000;
        return ans;
    }
    
    int par(Node A,Node B)//判断平行 
    {
        if((A.x2-A.x1)*(B.y2-B.y1)==(B.x2-B.x1)*(A.y2-A.y1)) return 1;
        return 0;
    }
    
    int ver(Node A,Node B)//判断垂直
    {
        if((A.x2-A.x1)*(B.x2-B.x1)+(A.y2-A.y1)*(B.y2-B.y1)==0)
        return 1;
        return 0;
    } 
    
    int check()
    {
        int i,m=0,n=0;
        for(i=1;i<4;i++)
            if(!f(a[0],a[i]))//无公共点
                break;
        for(int j=1;j<4;j++)
            if(j!=i)
            {
                if(m==0) m=j;
                else n=j;
            }
        if(i==4) return 0;
        
        int flag=f(a[0],a[n]);
        if(!(flag==1||flag==10||flag==100||flag==1000)) return 0;
        flag=f(a[n],a[i]);
        if(!(flag==1||flag==10||flag==100||flag==1000)) return 0;
        flag=f(a[i],a[m]);
        if(!(flag==1||flag==10||flag==100||flag==1000)) return 0;
        flag=f(a[0],a[m]);
        if(!(flag==1||flag==10||flag==100||flag==1000)) return 0;
        
        if(!(par(a[0],a[i])&&par(a[m],a[n]))) return 0;
        if(!(ver(a[0],a[n])&&ver(a[0],a[m]))) return 0;
        if(!(ver(a[i],a[n])&&ver(a[i],a[m]))) return 0;
        return 1;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--) 
        {
            for(int i=0;i<4;i++)
                scanf("%d%d%d%d",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
            if(!check()) printf("NO
    ");
            else printf("YES
    ");
        }
        return 0;
    }
    View Code




  • 相关阅读:
    [SQL SERVER] The CHECK_POLICY and CHECK_EXPIRATION options cannot be turned OFF when MUST_CHANGE is ON. (Microsoft SQL Server, Error: 15128)
    CENTOS7 SYSTEMD SERVICE 将自己的程序放入自动启动的系统服务
    CentOS7 关闭selinux
    面试总结TODO
    很好用的 UI 调试技巧
    点满 webpack 技能点,让你的打包速度提效 90%
    前端缓存最佳实践
    Fiddler抓包工具总结
    按钮粒子效果
    如何优雅的在 vue 中添加权限控制
  • 原文地址:https://www.cnblogs.com/ACRykl/p/9560613.html
Copyright © 2011-2022 走看看