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

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值