Description
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.
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 (
1N1, 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-coordinatesand
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
分析:分别对每行,即y相同的点求对称轴,即x的值,然后比较每行的轴是否一致即可;注意每行的轴即每两个点的轴,中间循环判断点之间的轴是否相等,每行处理前对x排序
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn = 1000 + 10;
typedef vector<int> pointx;
vector<pointx> dot;
map<int,int> IDcache;
void print()
{
for(int i=0; i<dot.size(); i++)
{
for(int j=0; j<dot[i].size(); j++)
{
printf("%4d",dot[i][j]);
}
putchar('\n');
}
}
int main()
{
int T,n;
cin >> T;
while(T--)
{
cin >> n;
int x,y;
dot.clear(); IDcache.clear();
for(int i=0; i<n; i++)
{
cin >> x >> y;
if(IDcache.count(y))
{
int index = IDcache[y];
dot[index].push_back(x);
}else{
pointx dotx;
dotx.push_back(x);
dot.push_back(dotx);
IDcache[y]=dot.size()-1;
}
}
int yes = 1;
int ave;
int avebefore;
for(int i=0; i<dot.size(); i++)
{
sort(dot[i].begin(),dot[i].end());
int dotisize = dot[i].size();
if(dotisize%2)ave=dot[i][dotisize/2]*2;
else {
ave=dot[i][0]+dot[i][dotisize-1];
}
if(i==0)avebefore=ave;
else if(avebefore!=ave){yes=0;break;}
int j=0;
for(j=0; j<dotisize/2; j++)
{
int avej=dot[i][j]+dot[i][dotisize-j-1];
if(avej!=ave){yes=0;break;}
}
}
printf("%s\n",yes?"YES":"NO");
}
return 0;
}