Symmetry

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-coordinates and 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


题意:
给出一张图,图上有一些点,你要做的就是找到一条线可以将这些点对称分开,如果能找到,输出yes,找不到输出no


思路:
最重要的一句“The figure on the right is not left-right symmetric as it is impossible to find such a vertical line”,vertical说明只需要垂直的竖线,那就好办多l
首先随便找到处于同一行的对称两点,通过它们求出中点,那么这条线就找到了。
然后枚举判断每一行的对称点到这条线的距离是否相等,如果不相等,马上break掉,输出no。
然后考虑数据结构,因为处在同一行的点你不知道会有多少,所以使用不定长数组vector会比较方便
再想到如果仅仅就以y为vector数组的下标,后面的枚举会不好进行,所以只需要开一个数组记录一下就好了,y的值是什么并不重要。
还有就是为了找到同一行对称两点,应该对那一行先排一下序,然后就好找了


代码:


#include"iostream" #include"algorithm" #include"cstring" #include"vector" using namespace std; const int maxn=10000+10; int a[10000+10000+10]; int main() { int t,n; cin>>t; while(t--) { int x,y,mid,top,flag; cin>>n; top=1; //top不能为0,因为if(!a[y]) a[y]=top++; flag=0; memset(a,0,sizeof(a)); vector<int> pile[maxn]; //这里不应该放在别处,每次输入n后都应该有一个全新的vector,以免受到上一次输入的影响 for(int i=0;i<n;i++) { cin>>x>>y; y+=10000; if(!a[y]) a[y]=top++; pile[a[y]].push_back(x); } sort(pile[1].begin(),pile[1].end()); mid=(pile[1][pile[1].size()-1]+pile[1][0])/2; for(int j=1;j<top;j++) { sort(pile[j].begin(),pile[j].end()); for(int k=0;k<pile[j].size();k++) if((pile[j][k]+pile[j][pile[j].size()-1-k])/2!=mid) flag=1; if(flag==1) break; } if(flag==1) { cout<<"NO"<<endl; } else cout<<"YES"<<endl; } return 0; } 

 

你可能感兴趣的:(try)