Four Segments CodeForces - 14C

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)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值