Blog original address: http://blog.csdn.net/xuechelingxiao/article/details/40680981
Poj 1859: The Perfect failed
Question:
Given the coordinates of N points, your task is to determine whether there is a center, so that the centers of these points are symmetric. The condition that a vertex set has a central symmetric point is that there is a vertex S. For each vertex P, another vertex Q exists so that P-S = s-Q.
Solution:
Now that we want to find the center point, there must be two symmetric points in the center. Then, after the point set is sorted, we first find the first and last vertices after sorting, find a center, take a point from the front, take a point from the back, and find a center. If the two centers are consistent, you can continue to take them. If they are not consistent, there is no center.
Just look at the code.
#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;struct Point { double x, y;} P[20010], o;int cmp(Point a, Point b) { if(a.x == b.x) return a.y < b.y; return a.x < b.x;}int main(){ int n; while(~scanf("%d", &n) && n) { bool flag = true; for(int i = 1; i <= n; ++i) { scanf("%lf%lf", &P[i].x, &P[i].y); } sort(P+1, P+n+1, cmp); o.x = (P[1].x+P[n].x)/2; o.y = (P[1].y+P[n].y)/2; for(int i = 2; i <= n; ++i) { double x = (P[i].x+P[n-i+1].x)/2; double y = (P[i].y+P[n-i+1].y)/2; if(x != o.x || y != o.y) { flag = false; //printf("%d\n", i); break; } } if(flag) { printf("V.I.P. should stay at (%.1lf,%.1lf).\n", o.x, o.y); } else { printf("This is a dangerous situation!\n"); } } return 0;}POJ 2526 :Center of symmetry
#include <stdio.h>#include <iostream>#include <algorithm>using namespace std;struct Point { double x, y;} P[20010], o;int cmp(Point a, Point b) { if(a.x == b.x) return a.y < b.y; return a.x < b.x;}int main(){ int n; int T; scanf("%d", &T); while(T--) { bool flag = true; scanf("%d", &n); for(int i = 1; i <= n; ++i) { scanf("%lf%lf", &P[i].x, &P[i].y); } sort(P+1, P+n+1, cmp); o.x = (P[1].x+P[n].x)/2; o.y = (P[1].y+P[n].y)/2; for(int i = 2; i <= n; ++i) { double x = (P[i].x+P[n-i+1].x)/2; double y = (P[i].y+P[n-i+1].y)/2; if(x != o.x || y != o.y) { flag = false; //printf("%d\n", i); break; } } if(flag) { printf("yes\n"); } else { printf("no\n"); } } return 0;}
Poj 1859 the perfect detection ry & poj2526 center of thinking)