UVA_1595: Symmetry
来源:互联网 发布:mysql find in set 编辑:程序博客网 时间:2024/05/03 09:36
Description
The figure shown on the left is left-right symmetric as it is possible to fold the sheet of paper along avertical 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 casesT is given in the first line of the input file. The first line of each test case contains an integerN , where N (1N1, 000) is the number of dots in a figure. Each of the following N lines contains the x-coordinate andy-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 105 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;}
0 0
- UVA_1595: Symmetry
- Symmetry
- Symmetry
- UVa1595 - Symmetry
- 1595 - Symmetry
- 对称轴(Symmetry)
- UVa1595 - Symmetry
- [UVA1595]Symmetry
- UVa1595-Symmetry
- rotational symmetry
- Symmetry UVA
- Symmetry CSU
- bzoj2592 Symmetry
- Symmetry UVA
- Centre of symmetry
- [1_6_checker] Bitmarks & Symmetry
- CodeForces 201A Symmetry
- UVa 1595 Symmetry
- 《学习日记》WEB缓存
- 重新签名APK文件(二)
- 括号配对问题
- Codeforces 582 B Once Again... (LIS)
- 【学习拓展】C语言 随机数应用:伪随机机制
- UVA_1595: Symmetry
- UI图标素材
- iOS之UI随笔-图片轮播器
- eclipse常用的几个快捷键
- 个人Linux学习笔记(三)
- 自己动手 IBeacon 室内定位 完整体验(超详细过程)
- Android,Java开发不能不知道的误区盲点
- 代码整洁之道读书笔记
- 小议hashmap