The figure shownon the left is left-right symmetric as it is possible to foldthe 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 notleft-right symmetric as it is impossible to find such a vertical line.
Write a programthat determines whether a figure, drawn with dots, is left-right symmetric ornot. The dots are all distinct.
The input consistsof T test cases. The number of test cases T isgiven in the first line of the input file. The first line of each test casecontains an integer N , where N ( 1N1, 000) is the number ofdots in a figure. Each of the following N lines containsthe x-coordinate and y-coordinate of a dot. Both x-coordinatesand y-coordinates are integers between -10,000 and 10,000, bothinclusive.
Print exactly oneline for each test case. The line should contain `YES' if the figure isleft-right symmetric. and `NO', otherwise.
The followingshows sample input and output for three test cases.
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
YES
NO
YES
代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
#include<cstring>
using namespacestd;
doubledot[1100][2];
int main()
{
int test;
cin>>test;
while(test--)
{
vector<double> vec;
map<pair<double,double>,int> map1;
memset(dot,0,sizeof(dot));
int num;
cin>>num;
for(int i=0;i<num;i++)
{
cin>>dot[i][0]>>dot[i][1];
vec.push_back(dot[i][0]);
map1[make_pair(dot[i][0],dot[i][1])]=i;
}
sort(vec.begin(),vec.end());
double x=(vec[0]+vec[vec.size()-1])/2;
bool flag=true;
for(int i=0;i<num;i++)
{
if(map1.count(make_pair(2*x-dot[i][0],dot[i][1]))==0)
{
cout<<"NO\n";
flag=false;
break;
}
}
if(flag)
{
cout<<"YES\n";
}
}
return 0;
}
由于规定了对称轴是垂直于X轴的,所以先求出X坐标,然后挨个判断。