您的位置:首页 > 其它

ZOJ.2540 Form a Square【水】 2015/10/01

2015-10-01 06:13 267 查看
Form a Square

Time Limit: 2 Seconds
Memory Limit: 65536 KB

Have you ever put up a tent on top of a mountain? The difficulty lies in finding the appropriate positions to fix the nails on the ground, which is a mostly exposed rock where we can hardly press the nails.

In our problem we have a square tent, which means we need to fix the four vertices on the ground. For some reason the size of the tent is not very important, but we need to accurately fix the vertices to form a square. Now we have picked four spots that
are suitable to press the nails and your job is to decide whether the spots form a square.

Input Description

Standard input will contain multiple test cases. The first line of the input is a single integer T (1 <= T <= 50) which is the number of test cases. T test cases follow, each preceded by a single blank line.

Each test case contains 4 lines for the coordinates of the four distinct vertices that are picked. Each line contains the x and y coordinates separated by a single space (0 < x, y < 1,000). But please notice that the coordinates are not necessarily given
in either clockwise or counterclockwise order for a square.

Output Description

Results should be directed to standard output. Start each case with "Case #:" on a single line, where # is the case number starting from 1. Two consecutive cases should be separated by a single blank line. No blank line should be produced after the last
test case.

The output should be either "Yes" or "No", indicating whether or not the four given positions can form a square.

Sample Input

3

1 1

1 2

2 1

2 2

1 1

2 2

3 3

4 4

1 2

2 1

2 3

3 2

Sample Output

Case 1:

Yes

Case 2:

No

Case 3:

Yes

Source: Asia 2005, Hangzhou (Mainland China), Preliminary

给出四个点,求这四个点能否构成正方形,即判断两点之间距离即可,但是给出点的顺序可能不是顺时针或逆时针的,直接暴力即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>

using namespace std;

struct point{
double x,y;
};

double dis(point a,point b){
return sqrt( (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y) );
}

int main(){
int t,ans=1;
bool flag = false;
point p[4];
scanf("%d",&t);
while(t--){
if(flag) printf("\n");
flag = true;
int num=1;
scanf("%lf%lf%lf%lf%lf%lf%lf%lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&p[2].x,&p[2].y,&p[3].x,&p[3].y);
double ret = 2000;
for( int i = 0 ; i < 4 ; ++i ){
for( int j = i+1 ; j < 4 ; ++j ){
double res = dis(p[i],p[j]);
if( res == ret )
num++;
else if( res < ret )
ret = res,num=1;
}
}
printf("Case %d:\n",ans++);
if( num == 4 )
printf("Yes\n");
else printf("No\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: