cf14C Four Segments(计算几何)

题意:

给四个线段(两个端点的坐标)。

判断这四个线段能否构成一个矩形。(矩形的四条边都平行于X轴或Y轴)

思路:

计算几何

代码:

class Point{
public:
    int x,y;
    void readd(int xx,int yy){
        x=xx;
        y=yy;
    }
};

class Segment{
public:
    Point startt,endd;
    int length;
    int Type; //1:横 2:竖 -1:斜
    void readd(int x1,int y1,int x2,int y2){
        startt.readd(x1,y1);
        endd.readd(x2,y2);
        length=(int)sqrt((double)(x2-x1)*(x2-x1)+(double)(y2-y1)*(y2-y1));
    }
    int typeCheck(){
        Type=-1;
        if(startt.x==endd.x){
            Type=2;
        }
        if(startt.y==endd.y){
            Type=1;
        }
    }
};
Segment S[5];

bool samePoint(Point a,Point b){
    if(a.x==b.x&&a.y==b.y){
        return true;
    }
    return false;
}

bool solve(){
    if(S[1].startt.y<S[2].startt.y){
        swap(S[1],S[2]);
    }
    if(S[1].startt.x>S[1].endd.x){
        swap(S[1].startt,S[1].endd);
    }
    if(S[2].startt.x>S[2].endd.x){
        swap(S[2].startt,S[2].endd);
    }

    if(S[3].startt.x>S[4].startt.x){
        swap(S[3],S[4]);
    }
    if(S[3].startt.y<S[3].endd.y){
        swap(S[3].startt,S[3].endd);
    }
    if(S[4].startt.y<S[4].endd.y){
        swap(S[4].startt,S[4].endd);
    }
    if(samePoint(S[1].startt,S[3].startt)&&
        samePoint(S[1].endd,S[4].startt)&&
        samePoint(S[3].endd,S[2].startt)&&
        samePoint(S[2].endd,S[4].endd)
       ){
            return true;
       }

    return false;
}

bool cmp1(Segment a,Segment b){
    return a.Type<b.Type;
}
int main(){

    rep(i,1,4){
        int x1,y1,x2,y2;
        cin>>x1>>y1>>x2>>y2;
        S[i].readd(x1,y1,x2,y2);
        S[i].typeCheck();
    }
    rep(i,1,4){
        if(S[i].length==0 || S[i].Type==-1){
            puts("NO");
            return 0;
        }
    }
    sort(S+1,S+5,cmp1);
    if(!(S[1].length==S[2].length&&S[3].length==S[4].length)){
        puts("NO");
        return 0;
    }
    if(solve()){
        puts("YES");
    }
    else{
        puts("NO");
    }
    return 0;
}
时间: 2024-10-26 19:51:34

cf14C Four Segments(计算几何)的相关文章

HDU 6697 Closest Pair of Segments (计算几何 暴力)

2019 杭电多校 10 1007 题目链接:HDU 6697 比赛链接:2019 Multi-University Training Contest 10 Problem Description The closest pair of points problem is a well-known problem of computational geometry. In this problem, you are given \(n\) points in the Euclidean plan

POJ 3304 Segments(计算几何)

题意:给定一些线段,问能否找出一条直线,穿过所有线段 思路:如果存在一条直线,那么必然有一条直线是过已有的两点,那么就枚举两点,然后去判断是否跟所有线段有交点即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; struct Point { double x, y; Point() {} Point(doub

POJ 3304 Segments(计算几何:直线与线段相交)

POJ 3304 Segments 大意:给你一些线段,找出一条直线能够穿过所有的线段,相交包括端点. 思路:遍历所有的端点,取两个点形成直线,判断直线是否与所有线段相交,如果存在这样的直线,输出Yes,但是注意去重. struct Point { double x, y; } P[210]; struct Line { Point a, b; } L[110]; double xmult(Point p1, Point p2, Point p) { return (p1.x-p.x)*(p2.

POJ 3304 Segments 【计算几何】【直线和线段的关系】

题目链接:http://poj.org/problem?id=3304 题目大意:T个case,每个case里面有N条线段,判断能否存在一条直线,使得所有的线段在这条直线上都能有公共点,如果存在输出Yes,否则输出No. 题目的意思可以变成,在N条直线的2*N个端点中选择两个点组成一条直线能否满足这个条件,暴力枚举即可,注意的一点是在枚举的2*n个点中每次选择的两个点要判断是不是重复的. #include<iostream> #include<stdio.h> #include&l

USACO6.5-Closed Fences:计算几何

Closed Fences A closed fence in the plane is a set of non-crossing, connected line segments with N corners (3 < N < 200). The corners or vertices are each distinct and are listed in counter-clockwise order in an array {xi, yi}, i in (1..N). Every pa

hdu 1086(计算几何入门题——计算线段交点个数)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7167    Accepted Submission(s): 3480 Problem Description Ma

【转】计算几何题目推荐

打算转下来好好做计算几何了. 原文地址:http://blog.sina.com.cn/s/blog_49c5866c0100f3om.html 其实也谈不上推荐,只是自己做过的题目而已,甚至有的题目尚未AC,让在挣扎中.之所以推荐计算几何题,是因为,本人感觉ACM各种算法中计算几何算是比较实际的算法,在很多领域有着重要的用途计算几何题的特点与做题要领:1.大部分不会很难,少部分题目思路很巧妙2.做计算几何题目,模板很重要,模板必须高度可靠.3.要注意代码的组织,因为计算几何的题目很容易上两百行

[POJ 3304]Segments

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14918   Accepted: 4728 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments

POJ 3304 计算几何

Segments Time Limit: 1000MS  Memory Limit: 65536K Total Submissions: 9564  Accepted: 2943 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments on