码迷,mamicode.com
首页 > 其他好文 > 详细

CDOJ 93 King's Sanctuary【判断四边形形状】

时间:2016-08-01 12:38:29      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

King‘s Sanctuary

Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)
 

The king found his adherents were building four sanctuaries for him. He is interested about the positions of the sanctuaries and wants to know whether they would form a parallelogram, rectangle, diamond, square or anything else.

Input

The first line of the input is TT(1T10001≤T≤1000), which stands for the number of test cases you need to solve. Each case contains four lines, and there are two integers in each line, which shows the position of the four sanctuaries. And it is guaranteed that the positions are given clockwise. And it is always a convex polygon, if you connect the four points clockwise.

Output

For every test case, you should output Case #t: first, where tt indicates the case number and counts from 11, then output the type of the quadrilateral.

Sample input and output

Sample Input Sample Output
5
0 0
1 1
2 1
1 0
0 0
0 1
2 1
2 0
0 0
2 1
4 0
2 -1
0 0
0 1
1 1
1 0
0 0
1 1
2 1
3 0
Case #1: Parallelogram
Case #2: Rectangle
Case #3: Diamond
Case #4: Square
Case #5: Others

Source

Sichuan State Programming Contest 2012


原题链接:http://acm.uestc.edu.cn/#/problem/show/93


题意:按顺时针(clockwise)方向给你四个点的坐标,判断它是否是平行四边形,矩形,菱形,正方形。


几何,肯定是用向量,具体判断四边形的形状,就看你初高中的几何了,具体判断,看下面这张图。

技术分享


AC代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cmath>
using namespace std;
struct point
{
    int x,y;
} p[4];
int main()
{
    int T;
    int kase=0;
    cin>>T;
    while(T--)
    {
        for(int i=0; i<4; i++)
        {
            cin>>p[i].x>>p[i].y;
        }
        int px= (p[1].x-p[0].x);
        int py= (p[1].y-p[0].y);

        int qx= (p[2].x-p[3].x);
        int qy= (p[2].y-p[3].y);

        int rx= (p[2].x-p[1].x);
        int ry=(p[2].y-p[1].y);

        int tx=p[3].x-p[0].x;
        int ty=p[3].y-p[0].y;
        printf("Case #%d: ",++kase);
        if(px==qx&&py==qy&&rx==tx&&ry==ty)//两组对边分别相等-->平行四边形
        {
            if(px*rx+py*ry==0)//+邻边垂直-->矩形或正方形
            {
                if(px*px+py*py==rx*rx+ry*ry)//+邻边相等-->正方形
                {
                    cout<<"Square"<<endl;
                }
                else
                    cout<<"Rectangle"<<endl;
            }
            else if(px*px+py*py==rx*rx+ry*ry)//+邻边不垂直但相等-->菱形
            {
                cout<<"Diamond"<<endl;
            }
            else//平行四边形
                cout<<"Parallelogram"<<endl;
        }
        else
            cout<<"Others"<<endl;
    }
    return 0;
}


CDOJ 93 King's Sanctuary【判断四边形形状】

标签:

原文地址:http://blog.csdn.net/hurmishine/article/details/52083635

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!