fzu 2035 Axial symmetry(几何)

Accept: 138    Submit: 470
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Axial symmetry is so beautiful. We can find many axial symmetric objects in everyday life. Following are some axial symmetric figures.

fzu 2035 Axial symmetry(几何)_第1张图片

Now, you are given a simple polygon. A simple polygon is a closed polygonal chain of line segments in the plane which do not have points in common other than the common vertices of pairs of consecutive segments.

To simplify the problem, the given simple polygon in this problem is special. All edges of the given polygon parallel to either X-axis or Y-axis. Your task is to examine whether the given polygon is axial symmetric.

 Input

The first line of the input contains an integer T(T≤50), indicating the number of test cases. Each case begins with one integer n(10≤n≤500), the number of points. The next n lines indicate the points of the polygon, each with two integers x(-100,000≤x≤100,000) and y(-100,000≤y≤100,000). The points would be given either clockwise or counterclockwise.

 Output

For each test case, print a line containing the test case number (beginning with 1) and if the polygon is axial symmetric, please output “YES”, or you should output “NO”.

 Sample Input

240 00 11 11 060 04 04 21 21 40 4

 Sample Output

Case 1: YESCase 2: NO

 Source

2011年全国大学生程序设计邀请赛(福州)

题意:顺时针给定一些点,判断该n边形是否对称。

思路:由于点是顺逆时针输入,所以可以把每个点和边中点按顺序保存,然后对称轴必然由i,i + n组成,枚举对称轴O(n),然后在枚举每个点的对称点是否在点集中即可。

代码:

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <set>
using namespace std;

const int N = 505;

int t, n;
struct Point {
    double x, y;
} p[N], save[2 * N];

typedef pair<double, double> pii;
set<pii> point;

void init() {
    point.clear();
    scanf("%d", &n);
    for (int i = 0; i < n; i ++) {
	scanf("%lf%lf", &p[i].x, &p[i].y);
	point.insert(make_pair(p[i].x, p[i].y));
    }
    for (int i = 0; i < n - 1; i ++) {
	save[i * 2].x = p[i].x;
	save[i * 2].y = p[i].y;
	save[i * 2 + 1].x = (p[i].x + p[i + 1].x) / 2;
	save[i * 2 + 1].y = (p[i].y + p[i + 1].y) / 2;
    }
    save[(n - 1) * 2].x = p[n -1].x;
    save[(n - 1) * 2].y = p[n -1].y;
    save[(n - 1) * 2 + 1].x = (p[n - 1].x + p[0].x) / 2;
    save[(n - 1) * 2 + 1].y = (p[n - 1].y + p[0].y) / 2;
}

void line(double &A, double &B, double &C, Point p1, Point p2) {
    A = p1.y - p2.y;
    B = p2.x - p1.x;
    C = -(A * p1.x + B * p1.y);
}

void tra(double A, double B, double C, Point p1, Point &p2) {
    p2.x = p1.x - (2 * A * (A * p1.x + B * p1.y + C)) / (A * A + B * B);
    p2.y = p1.y - (2 * B * (A * p1.x + B * p1.y + C)) / (A * A + B * B);
}

bool judge(double A, double B, double C) {
    Point p0;
    for (int i = 0; i < n; i += 2) {
	tra(A, B, C, p[i], p0);
	//printf("%lf %lf %lf %lf %lf %lf %lf\n", A, B, C, p[i].x, p[i].y, p0.x, p0.y);
	if (point.find(make_pair(p0.x, p0.y)) == point.end())
	    return false;
    }
    return true;
}

bool solve() {
    double A, B, C;
    for (int i = 0; i < n; i ++) {
	line(A, B, C, save[i], save[i + n]);
	if (judge(A, B, C))
	    return true;
    }
    return false;
}

int main() {
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
	init();
	printf("Case %d: ", ++cas);
	if (solve()) printf("YES\n");
	else printf("NO\n");
    }
    return 0;
}



你可能感兴趣的:(fzu 2035 Axial symmetry(几何))