Symmetry 解题心得

---恢复内容开始---

Description

The figure shown on the left is left-right symmetric as it is possible to fold the sheet of paper along a vertical line, drawn as a dashed line, and to cut the figure into two identical halves. The figure on the right is not left-right symmetric as it is impossible to find such a vertical line.

Write a program that determines whether a figure, drawn with dots, is left-right symmetric or not. The dots are all distinct.

Input

The input consists of T test cases. The number of test cases T is given in the first line of the input file. The first line of each test case contains an integer N , where N ( 1N1, 000) is the number of dots in a figure. Each of the following N lines contains the x-coordinate and y-coordinate of a dot. Both x-coordinates and y-coordinates are integers between -10,000 and 10,000, both inclusive.

Output

Print exactly one line for each test case. The line should contain `YES‘ if the figure is left-right symmetric. and `NO‘, otherwise.

The following shows sample input and output for three test cases.

Sample Input

3
5
-2 5
0 0
6 5
4 0
2 3
4
2 3
0 4
4 0
0 0
4
5 14
6 10
5 10
6 14

Sample Output

YES
NO
YES

分析:我的思路是输入的同时,记下某一行的x的最大值和最小值,通过这一行的对称轴,再默认已它为所有行的对称轴去判断是否适用其他行,没有冲突即找到对称轴。

我的代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

struct point
{
    double x, y;

    point(int x1 = 0, int y1 = 0) :x(x1), y(y1)
    {}

    void mirror()
    {
        x = -x;
    }

    bool operator <(const point &b)const
    {
        if (x == b.x){
            return  (y< b.y);
        }
        else{
            return x < b.x;
        }
    }

}p[1010];

map<point, bool> m;

bool findmirror(point & p1, double x)
{
    point tempp;
    tempp.x = 2 * x - p1.x;
    tempp.y = p1.y;
    //auto result =m.find(tempp);
    if (m[tempp]){
        return true;
    }
    return false;
}

int Max = -15000;
int Min = 15000;

int main()
{
    int t;
    cin >> t;
    while (t--){
        m.clear();
        bool flag = true;
        Max = -15000;
        Min = 15000;
        int n;
        cin >> n;

        //输入
        for (int i = 0; i < n; i++){
            cin >> p[i].x >> p[i].y;
            m[p[i]] = true;
            if (p[i].y == p[0].y){
                if (Max < p[i].x)                Max = p[i].x;
                if (Min>p[i].x)                Min = p[i].x;
            }
        }

    //判断
            double mir = (double)(Max + Min) / 2;
            for (int i = 0; i < n; i++){
                if (  !findmirror(p[i], mir)      ){
                    flag = false;
                    break;
                }
            }
            if (flag == true)
                puts("YES");
            else
                puts("NO");
        }

    return 0;
}

时间: 2024-10-27 06:52:07

Symmetry 解题心得的相关文章

第四章学习小结 串的模式匹配 解题心得体会

串的模式匹配 解题心得体会 关于串,模式匹配是其一个很重要的问题.针对这个问题,书上讲了两种模式匹配的算法,即BF算法和KMP算法,下面针对这两种算法的实现谈谈我的心得. 一.BF算法的探索 [错误代码1] #include<iostream> #include<string.h> using namespace std; typedef struct{ char ch[1000002]; int length; }SString; void Index_BF(SString S,

括号配对问题——解题心得

Description You are given a string consisting of parentheses () and []. A string of this type is said to be correct: (a)if it is the empty string (b)if A and B are correct, AB is correct, (c)if A is correct, (A ) and [A ] is correct. Write a program

wechall.net/stegano 解题心得

最近迷上了 www.wechall.net 网站,里面都是些与计算机相关的题目挑战.题目又分很多类型,例如:加密与解密.隐写术.网络攻防.趣味编程.数学逻辑等.题目有的简单,有的很难,需要一些知识和技巧.与其他题目挑战的网站不同的是,在其他类似性质的网站注册的用户可以绑定到 WeChall 网站,然后 WeChall 提供排名信息,而且也分得很细,什么按总分全球排名.什么在自己国家的排名.什么解答某种语言网站题目的排名等.可以从解题的人数判断题目的难易程度,有兴趣的朋友可以去注册,解题中也能学到

Ducci Sequence 解题心得

原题贴上 A Ducci sequence is a sequence of n-tuples of integers. Given an n-tuple of integers (a1, a2, ... , an), the next n-tuple in the sequence is formed by taking the absolute differences of neighboring integers: ( a1, a2, ... , an)  (| a1 - a2|,| a2

HDU 4627 The Unsolvable Problem 解题心得

原题: Description There are many unsolvable problem in the world.It could be about one or about zero.But this time it is about bigger number. Given an integer n(2 <= n <= 10 9).We should find a pair of positive integer a, b so that a + b = n and [a, b

USACO Greedy Gift Givers 解题心得

本题算法不难想出,但是中间还是出现了一些问题. 开始的时候是#11:Execution error,后来把普通的数组改成动态数组后问题消失. 后来又出现了Execution error: Your program had this runtime error: Illegal file open (/dev/tty). 随后google解决方案,多数都是数组开小了.遂开大数组,无效. 突然意识到很有可能是低级错误,于是检查代码. 发现写了个 for(int i = 0; i < n2 ; i++

UVa 1647 - Computer Transformation 解题心得

这个题目.... 想上题意 10935 Throwing cards away I Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n at the bottom. The following operation is performed as long as there are at least two cards in the deck: Throw away the to

Codeforces Round #315 (Div. 2) A. Music 解题心得

原题: Description Little Lesha loves listening to music via his smartphone. But the smartphone doesn't have much memory, so Lesha listens to his favorite songs in a well-known social network InTalk. Unfortunately, internet is not that fast in the city

UVa 10382 - Watering Grass 解题心得

原题: n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance from the left end of t