Uva1595 Symmetry 【set集合】【習題5-6】
阿新 • • 發佈:2019-01-06
題意:給出一堆點,是否能找出一條對稱軸!
思路:首先計算出對稱軸:(最左邊的橫座標+最右邊的橫座標)/ 2 因為存在對稱軸的話肯定是以倆個端點的對稱軸為目標
然後遍歷所有點,看每個點是否有它所對稱的那個點,這個用set查詢,提前輸入時將所有點都放入set集合中
如果都能找到即為YES否則NO;
自己沒有想到。。。參考的
程式碼:
#include <iostream> #include <set> #include <algorithm> #include <vector> #include <math.h> #include <sstream> #include <fstream> #define FILE #define maxn 1005 using namespace std; typedef pair<int,int> point; set<point>coord; int main() { //檔案輸入輸出,保留 /*#ifdef FILE ifstream in("data.txt"); ofstream out("output.txt"); cin.rdbuf(in.rdbuf()); cout.rdbuf(out.rdbuf()); #endif*/ int t,n,x[maxn],y[maxn]; cin >> t; while(t--) { coord.clear(); cin >> n; int symmetry = 0,leftP = 99999,rightP = -99999; for(int i=0;i<n;i++) { cin >> x[i] >> y[i]; x[i] *= 2;//給橫座標*2為了避免對稱軸為小數 symmetry += x[i]; coord.insert(point(x[i],y[i])); leftP = min(leftP,x[i]); rightP = max(rightP,x[i]); } symmetry = (leftP+rightP)/2;//計算對稱軸 //for(set<point>::iterator it=coord.begin();it!=coord.end();it++ ) // cout << it->first << "--" << it->second <<endl; bool flag = true; for(int i=0;i<n;i++) { if(x[i] == symmetry) continue; int distances = 2*abs(x[i] - symmetry); if(x[i] < symmetry)//左邊 { if(coord.find(point(x[i] + distances,y[i])) == coord.end()) { flag = false; break; } } else//右邊 { if(coord.find(point(x[i] - distances,y[i])) == coord.end()) { flag = false; break; } } } if(flag) cout << "YES" <<endl; else cout << "NO" << endl; } return 0; }