http://vjudge.net/contest/view.action?cid=47975#problem/C
Description
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».
Sample Input
1 1 6 1 1 0 6 0 6 0 6 1 1 1 1 0
YES
0 0 0 3 2 0 0 0 2 2 2 0 0 2 2 2
NO
注意:给定的四条线段不一定可以围成一个四边形。
答题思路:统计出现的八个点的个数,每个点重复两次。在四边形中每一个点和其相邻的两个点的一个横坐标以纵坐标相同。
#include <stdio.h>
struct point
{
int x,y;
} p[10];
int main()
{
int i,j,t,f;
int a[10];
while(~scanf("%d%d%d%d",&p[0].x,&p[0].y,&p[1].x,&p[1].y))
{
int cnt=0;
f=1;
if(p[0].x==p[1].x)
cnt+=1;
else if(p[0].y==p[1].y)
cnt+=10;
else
f=0;
a[0]=a[1]=0;
for(i=2; i<8; i+=2)
{
scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i+1].x,&p[i+1].y);
if(p[i].x==p[i+1].x)
cnt+=1;
else if(p[i].y==p[i+1].y)
cnt+=10;
else
f=0;
a[i]=a[i+1]=0;
}
if(cnt!=22)
f=0;
for(i=0; i<8; i++)
{
for(j=0; j<8; j++)
{
if(p[i].x==p[j].x&&p[i].y==p[j].y)
a[i]++;
}
}
for(i=0; i<8; i++)
if(a[i]!=2)
f=0;
if(f)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}