Symmetry 解题心得

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

Description

Download as PDF
 

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.

 

\epsfbox{p3226.eps}

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 ( 1$ \le$N$ \le$1, 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;
}
View Code

 

 

posted @ 2015-07-18 14:15  Shawn_Ji  阅读(170)  评论(0编辑  收藏  举报