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

UVA 10585 Center of symmetry

时间:2015-03-04 18:16:34      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

题意:给出一个点集,问这个集合有没有中心点使点集对称,这个点可以是点集中的点也可以不是点集的点。

解法:一开始我枚举每两个点连线的中点……结果T了orz当时也不知道怎么想的……

将点按横坐标排序,如果点集有中心点则中心点一定是排序后排在中间的那个点(n为奇数)或者中间两个点的连线中点(n为偶数),然后判断一下是不是中心点即可。

代码:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
struct node
{
    int x, y;
    bool operator < (const node& tmp) const
    {
        if(x == tmp.x)
            return tmp.y < y;
        return tmp.x < x;
    }
} point[10005];
map <node, int> m;//打一个点的表
int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        m.clear();
        int n;
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d", &point[i].x, &point[i].y);
            m.insert(pair <node, int> (point[i], 1));
        }
        int ans = 1;
        sort(point, point + n);
        node center;
        if(n & 1)
        {
            center.x = point[n / 2].x * 2;
            center.y = point[n / 2].y * 2;
        }
        else
        {
            center.x = point[n / 2 - 1].x + point[n / 2].x;
            center.y = point[n / 2 - 1].y + point[n / 2].y;
        }
        for(int k = 0; k < n; k++)//判断是否每个点都能找到对应对称点
        {
            node tmp;
            tmp.x = center.x - point[k].x;
            tmp.y = center.y - point[k].y;
            if(m.find(tmp) == m.end())
            {
                ans = 0;
                break;
            }
        }
        if(ans)
            printf("yes\n");
        else
            printf("no\n");
    }
    return 0;
}

 

UVA 10585 Center of symmetry

标签:

原文地址:http://www.cnblogs.com/Apro/p/4313718.html

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