紫书第五章习题 5-6 UVa 1595 Symmetry(暴力)

出处:https://blog.csdn.net/acvay/article/details/43015507
题目链接:https://vjudge.net/contest/231030#problem/F

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

思路:暴力枚举可以过  如果存在对称轴的话那么对称轴的横坐标一定是最左边的点和最右边的点的中点   为了避免中点是小数  可以将横坐标都乘上2  然后在判断所有点是否有对称点就行了

#include <bits/stdc++.h>  
using namespace std;  
const int N = 1005;  
int x[N], y[N], n, mx;  
  
bool have(int i)  
{  
    for(int j = 0; j < n; ++j)  
        if(y[i] == y[j] && x[i] + x[j] == 2 * mx) return true;  
    return false;  
}  
  
int main()  
{  
    int cas, lx, rx, a, i;  
    scanf("%d", &cas);  
    while(cas--)  
    {  
        lx = rx = 0;  
        scanf("%d", &n);  
        for(i = 0; i < n; ++i)  
        {  
            scanf("%d%d", &a, &y[i]);  
            x[i] = a * 2;  
            if(x[i] < x[lx]) lx = i;  
            if(x[i] > x[rx]) rx = i;  
        }  
        mx = (x[lx] + x[rx]) / 2;  
        for(i = 0; i < n; ++i)  
            if(!have(i)) break;  
  
        if(i >= n) puts("YES");  
        else puts("NO");  
    }  
    return 0;  
}  

猜你喜欢

转载自blog.csdn.net/JXUFE_ACMer/article/details/80454789