Several months later Alex finally got his brother Bob’s creation by post. And now, in his turn, Alex wants to boast about something to his brother. He thought for a while, and came to the conclusion that he has no ready creations, and decided to write a program for rectangles detection. According to his plan, the program detects if the four given segments form a rectangle of a positive area and with sides parallel to coordinate axes. As Alex does badly at school and can’t write this program by himself, he asks you to help him.
Input
The input data contain four lines. Each of these lines contains four integers x1, y1, x2, y2 ( - 109 ≤ x1, y1, x2, y2 ≤ 109) — coordinates of segment’s beginning and end positions. The given segments can degenerate into points.
Output
Output the word «YES», if the given four segments form the required rectangle, otherwise output «NO».
Examples
Input
1 1 6 1
1 0 6 0
6 0 6 1
1 1 1 0
Output
YES
Input
0 0 0 3
2 0 0 0
2 2 2 0
0 2 2 2
Output
NO
题意:给定四个线段,问能够形成一个平行于坐标轴的矩形吗。
思路很简单,但是有两个坑点。
①矩形必须是平行于坐标轴的。
②给定的有可能不是线段,有可能是点。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
struct node{
int a1,b1,a2,b2;
}p[5];
inline int check(node xx,node yy)
{
int s1=xx.a1-xx.a2;
int s2=xx.b1-xx.b2;
int s3=yy.a1-yy.a2;
int s4=yy.b1-yy.b2;
if(s1==0&&s4==0||s2==0&&s3==0) return 1;
else if(s2*s4+s1*s3==0) return 1;
return 0;
}
inline void dfs(int u,int x,int y,int &flag,int cnt)//先确定一个点,dfs去找另一个端点也是这个点的线段,找4次就找完了。
{
if(!flag||cnt>4) return ;
for(int i=1;i<=4;i++)
{
if(i==u) continue;
if(p[i].a1==x&&p[i].b1==y)
{
if(check(p[i],p[u])==0)
{
flag=0;
return ;
}
dfs(i,p[i].a2,p[i].b2,flag,cnt+1);
return ;
}
else if(p[i].a2==x&&p[i].b2==y)
{
if(check(p[i],p[u])==0)
{
flag=0;
return ;
}
dfs(i,p[i].a1,p[i].b1,flag,cnt+1);
return ;
}
}
flag=0;return ;
}
int main()
{
for(int i=1;i<=4;i++) cin>>p[i].a1>>p[i].b1>>p[i].a2>>p[i].b2;
int flag=1;
for(int i=1;i<=4;i++)
{
if(p[i].a1-p[i].a2&&p[i].b1-p[i].b2||p[i].a1==p[i].a2&&p[i].b1==p[i].b2)
{
flag=0;break;
}
}
dfs(1,p[1].a1,p[1].b1,flag,0);
if(flag) puts("YES");
else puts("NO");
return 0;
}
努力加油a啊,(o)/~