uva 1595 Symmetry“结构体”

给出平面上N(N<=1000)个点。问是否可以找到一条竖线,使得所有点左右对称,如图所示:

则左边的图形有对称轴,右边没有。
 
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



写题永远都是wrong answer.....
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;

const int maxn = 1010; 

struct point{
    double x, y;

    bool operator  < (const point & rhs) const{
        return x < rhs.x;
    }
}p[maxn];


int main()
{
    int T;
    cin >> T;
    while(T--){
        int n;
        double line;                         //line 为对称轴的位置
        bool flag = true;

        cin >> n;

        for(int i = 0;i < n; i++){
            double x, y;
            cin >> x >> y;
            p[i].x = x;
            p[i].y = y;
        }

        sort(p,p+n);

        for(int i = 0; i < n/2; i++){
            int k = 0;
            for(int j = n-1; j>n/2-1;j--){
                cout<<"** "<<j<<" "<<p[i].y<<" "<<p[j].y<<endl;
                if(p[i].y == p[j].y){
                    p[j].y = -0.111;
                    k = 1;
                    if(i == 0)line = ( p[i].x + p[n-1-i].x ) / 2.0;
                    else {
                        double l = ( p[i].x + p[j].x ) / 2.0;
                        cout<< p[i].x<<" - "<<p[j].x<<endl;
                        if(l != line){
                            cout << 1 <<" "<< l<< " "<< line<<endl;
                            flag = false;
                            break;
                        }
                    }
                    cout<<"line "<<line<<endl;
                    break;
                }
            }
            
            if(k){
                if(!flag)break;
            }
            else {
                flag = false;
                break;
            }
            

        }

        if(flag && (n%2)){            
            if(p[n/2].x != line )flag = false;
            cout<<3<<" "<< p[n/2].x<<" "<<line<<endl;    
        }

        if(flag) cout << "YES" << endl;
        else cout << "NO" << endl;

    }
    system("pause");
    return 0;
}

 

 

那就找错。。。

以上代码没有考虑到对称轴上有去多点的情况

将点排序后,先分偶数奇数个点两种情况,根据最中间的两个点或一个点计算出对称轴再进一步判断其他点是否符合

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

const int maxn = 1010; 

struct point{
    double x, y;

    bool operator  < (const point & rhs) const{
        return x < rhs.x;
    }
}p[maxn];

int main()
{
    int T;
    cin >> T;
    while(T--){
        int n ,n0 ;
        double line;                         //line 为对称轴的位置
        bool flag = true;

        cin >> n;

        for(int i = 0;i < n; i++){
            double x, y;
            cin >> x >> y;
            p[i].x = x;
            p[i].y = y;
        }

        sort(p,p+n);

        if(n%2){                                   //奇数个点
            line = p[n/2].x;
            n0 = n/2;
        }
        else {                                    //偶数个点  
            line = (p[n/2].x+p[n/2-1].x)/2.0;  
            n0 = n/2-1;
        }

        for(int i = 0; i <= n0; i++){
            int k = 0;
            for(int j = n-1; j>n/2-1;j--){
                if(p[i].y == p[j].y){
                        p[j].y = -0.111;
                        k = 1;
                        double l = ( p[i].x + p[j].x ) / 2.0;

                        if(l != line){
                            flag = false;
                            break;
                        }

                    break;
                }
                else{
                    if(p[i].x == line&&p[n-i-1].x == line){
                        k = 1;
                        break;
                    }
                }
                        
            }
            
            if(k){
                if(!flag)break;
            }
            else {
                flag = false;
                break;
            }
            

        }

        if(flag) cout << "YES" << endl;
        else cout << "NO" << endl;

    }
    //system("pause");
    return 0;
}

 

 

posted @ 2016-05-10 19:14  Not-Bad  阅读(280)  评论(0编辑  收藏  举报