uva 1595 - Symmetry

点对称性检测算法实现

思路:首先,如果这些点对称,那么它们的对称轴是x = m(m是所有点横坐标的平均值);

     把这些点放到一个集合里,然后扫描每个点,计算出它关于x = m的对称点,看这个点是否在集合里面。

     如果有一个不在的话,说明不能构成对称图形。

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <set>
 5 using namespace std;
 6 
 7 struct Point{
 8     int x, y;
 9     Point(int x = 0, int y = 0):x(x), y(y){}
10     bool operator < (const Point & rhs) const
11     {
12         return x < rhs.x || (x == rhs.x && y < rhs.y);
13     }
14 };
15 
16 const int maxn = 1000 + 10;
17 Point p[maxn];
18 
19 int main()
20 {
21     int T, n;
22     set<Point> sP;
23     scanf("%d", &T);
24     while(T--)
25     {
26         double s = 0, m;
27         sP.clear();
28         scanf("%d", &n);
29         for(int i = 0; i < n; i++)
30         {
31             cin >> p[i].x >> p[i].y;
32             sP.insert(p[i]);
33             s += p[i].x;
34         }
35         m = s / n;    // m: must to be a integer.
36         bool ok = true;
37         for(int i = 0; i < n; i++)
38         {
39             Point t((int)(m*2)-p[i].x, p[i].y);
40             if(!sP.count(t))
41             {
42                 ok = false;
43                 break;
44             }
45         }
46         printf("%s\n", ok ? "YES" : "NO");
47     }
48     return 0;
49 }

 

转载于:https://www.cnblogs.com/aze-003/p/5117114.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值