1. 程式人生 > >ZOJ.2540 Form a Square【水】 2015/10/01

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


相關推薦

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

ZOJ.2835 Magic Square 2015/09/23

Magic Square Time Limit: 2 Seconds      Memory Limit: 65536 KB In recreational mathematics, a magic square of n-degree is an arrangem

ZOJ.2679 Old Bill 2015/10/12

Old Bill Time Limit: 2 Seconds      Memory Limit: 65536 KB Among grandfather��s papers a bill was found: 72 turkeys $_679_ The first

Poj.3100 Root of the Problem 2015/09/22

The input consists of one or more pairs of values for B and N. Each pair appears on a single line, delimited by a single space. A line specifying the valu

HDU2075 A|B?

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 24999 Accepted Submission(s):

矩陣 A+B

在數學中,矩陣是一個按照長方陣列排列的複數或實數集合,最早來自於方程組的係數及常數所構成的方陣。這一概念由19世紀英國數學家凱利首先提出。 矩陣是高等代數學中的常見工具,也常見於統計分析等應用數學學科中。在物理學中,矩陣於電路學、力學、光學和量子物理中都有應用;電腦科學中,三維動畫製作也需要用到矩陣。 矩陣

hdoj 1518 Square dfs

efi string.h pre lan .cn rac article 長度 trac 題意:給出n個(不同長度的)棍子,問能不能將他們構成一個正方形。 策略:深搜。 hdoj 1455的簡化版 代碼: #include <stdio.h>

345. Reverse Vowels of a Stringeasy

not blog ive 參考 char wap ast otc emp 345. Reverse Vowels of a String【easy】 Write a function that takes a string as input and reverse o

HDU1203 I NEED A OFFER! 貪心

tis -m mem 概率 全部 itl ++ tput amp I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 6553

18.2.14 codevs1430 素數判定

兩個 技術分享 display for 素數 onclick play mes ber 題目描述 Description 質數又稱素數。指在一個大於1的自然數中,除了1和此整數自身外,不能被其他自然數整除的數。 素數在數論中有著很重要的地位。比1大但不是素數的數

18.2.14 codevs1474 十進制轉m進制

sed mat pos post () play 反向 分享圖片 scrip 題目描述 Description 將十進制數n轉換成m進制數 m<=16 n<=100 輸入描述 Input Description 共一行 n和m

18.2.14 codevs1475 m進制轉十進制

圖片 += ostream 一個數 gif mes body char ios 題目描述 Description 將m進制數n轉化成一個十進制數 m<=16 題目保證轉換後的十進制數<=100 輸入描述 Input Descriptio

4月21日模擬題解

code 水題 pan ret 學生 歸並 模擬題 方法 題解 (((((((((((((((((((((((((((((((((((((((((((((((((((((4.21模擬題解((((((((((((((((((((((((((((((((((((((((((((

Codeforces 963B Destruction of a Tree 貪心

UC nbsp span include end 準備 cto stream 父親節 本題的貪心策略是:每次刪除連到葉子結點的dfs鏈上離根最遠的偶數度的結點 greed is good 實現方法是先維護一個degree[i]表示第i個點有多少個度,然後dfs,當每一個結點

HDU 2102 A計劃 BFS

node 二層 output bool pan set spa 右移 als <題目鏈接> 題目大意: 可憐的公主在一次次被魔王擄走一次次被騎士們救回來之後,而今,不幸的她再一次面臨生命的考驗。魔王已經發出消息說將在T時刻吃掉公主,因為他聽信謠言說吃公主的肉也能

ZYH記

餐廳 spa 電腦 爭辯 放心 還記得 滿了 後來 一個   我從十二歲起,便在sdoj的蒟蒻餐廳裏當夥計,root說,樣子太傻,怕侍候不了專職切題,就在外面做點事罷。外面的調試管理,雖然容易說話,但嘮嘮叨叨纏夾不清的也很不少。他們往往要親眼看著一個字一個字編譯過,看過到底

PAT甲1115 Counting Nodes in a BSTdfs

1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following prope

非原創ZOJ - 4062 Plants vs. Zombies二分

題目:戳這裡 題意:機器人走過一個花,可以給那個花澆水,給定步數下,問花的最小的最大能量值。 學習部落格:戳這裡 本人程式碼: 1 #include <bits/stdc++.h> 2 typedef long long ll; 3 const int maxn

1021: Tom數-----ZZUOJ

1021: Tom數 Time Limit: 1 Sec   Memory Limit: 128 MB Submit: 31   Solved: 10 [ Subm

BZOJ1113 Poi2008 海報PLA單調棧

BZOJ1113 Poi2008 海報PLA Description N個矩形,排成一排. 現在希望用盡量少的矩形海報Cover住它們. Input 第一行給出數字N,代表有N個矩形.N在[1,250