您的位置:首页 > 其它

ZOJ-1179

2014-07-19 11:19 281 查看
矩形判断,先预排序一下,然后枚举所有四个点矩形组合判断下就行了

#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<iostream>

using namespace std;

namespace
{
struct Point
{
char c;
int x, y;
};

bool cmp(const Point *p1, const Point *p2)
{
if (p1->x != p2->x)
return p1->x < p2->x;
else
return p1->y < p2->y;
}

bool valid(Point *p1, Point *p2, Point *p3, Point *p4)
{
return p1->x == p2->x && p3->x == p4->x && p1->y == p3->y
&& p2->y == p4->y;
}
}

int main()
{
int n, x, y, count = 0;
vector<Point*> V;
vector<string> res;
char s[2];
while (scanf("%d", &n), n)
{
getchar();
V.clear();
res.clear();
for (int i = 0; i < n; i++)
{
scanf("%s %d %d", s, &x, &y);
Point *p = new Point();
p->c = s[0];
p->x = x;
p->y = y;
V.push_back(p);
}
sort(V.begin(), V.end(), cmp);
for (size_t i = 0; i < V.size(); i++)
for (size_t j = i + 1; j < V.size(); j++)
for (size_t k = j + 1; k < V.size(); k++)
for (size_t l = k + 1; l < V.size(); l++)
if (valid(V[i], V[j], V[k], V[l]))
{
string str;
str.push_back(V[j]->c);
str.push_back(V[l]->c);
str.push_back(V[k]->c);
str.push_back(V[i]->c);
res.push_back(str);
}
sort(res.begin(), res.end());
printf("Point set %d:", ++count);
if (res.size())
{
for (size_t i = 0; i < res.size(); i++)
{
if (i % 10 == 0)
putchar('\n');
cout << ' ' << res[i];
}
putchar('\n');
}
else
puts(" No rectangles");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: