请问sicily 1004.I Conduit!求代码
来源:学生作业帮助网 编辑:作业帮 时间:2024/11/19 11:20:02
请问sicily 1004.I Conduit!求代码
请问sicily 1004.I Conduit!求代码
请问sicily 1004.I Conduit!求代码
/* source code of submission 181146, Zhongshan University Online Judge System */
#include
#include
#include
using namespace std;
struct Line
{
double x1, y1, x2, y2, dist, xielv;
};
Line l[10000];
int n;
inline bool Equal(double a, double b)
{
return fabs(a - b) < 1e-6;
}
inline bool EG(double a, double b)
{
return a > b || Equal(a, b);
}
bool cmp(Line l1, Line l2)
{
if(!Equal(l1.xielv, l2.xielv))
return l1.xielv < l2.xielv;
else if(!Equal(l1.dist, l2.dist))
return l1.dist < l2.dist;
else if(!Equal(l1.y1, l2.y1))
return l1.y1 < l2.y1;
else
return l1.x1 < l2.x1;
}
void Init()
{
int i;
for(i=0; i
scanf("%lf%lf%lf%lf", &l[i].x1, &l[i].y1, &l[i].x2, &l[i].y2);
if(!Equal(l[i].y1, l[i].y2))
{
if(l[i].y1 > l[i].y2)
{
swap(l[i].y1, l[i].y2);
swap(l[i].x1, l[i].x2);
}
}
else
{
if(l[i].x1 > l[i].x2)
{
swap(l[i].y1, l[i].y2);
swap(l[i].x1, l[i].x2);
}
}
if(Equal(l[i].x1, l[i].x2))
{
l[i].xielv = 1e30;
l[i].dist = l[i].x1;
}
else
{
l[i].xielv = (l[i].y1 - l[i].y2) / (l[i].x1 - l[i].x2);
l[i].dist = (l[i].x1*l[i].y2 - l[i].y1*l[i].x2) / (l[i].x1 - l[i].x2);
}
}
}
inline double Max(double x, double y)
{
return x < y ? y : x;
}
inline double Min(double x, double y)
{
return x > y ? y : x;
}
inline bool OnLine(Line l, double x, double y)
{
return EG(x, Min(l.x1, l.x2)) && EG(Max(l.x1, l.x2), x) && EG(y, Min(l.y1, l.y2)) && EG(Max(l.y1, l.y2), y);
}
void Print()
{
int i;
for(i=0; i
printf("%.2lf %.2lf %.2lf %.2lf\n", l[i].x1, l[i].y1, l[i].x2, l[i].y2);
}
}
void Compute()
{
sort(l, l+n, cmp);
// Print();
int i, cnt = n;
for(i=1; i
if(Equal(l[i].xielv, l[i-1].xielv) && Equal(l[i].dist, l[i-1].dist) && (OnLine(l[i], l[i-1].x1, l[i-1].y1) || OnLine(l[i], l[i-1].x2, l[i-1].y2) || OnLine(l[i-1], l[i].x1, l[i].y1) || OnLine(l[i-1], l[i].x2, l[i].y2)))
{
cnt--;
double minx = Min(l[i].x1, Min(l[i].x2, Min(l[i-1].x1, l[i-1].x2)));
double miny = Min(l[i].y1, Min(l[i].y2, Min(l[i-1].y1, l[i-1].y2)));
double maxx = Max(l[i].x1, Max(l[i].x2, Max(l[i-1].x1, l[i-1].x2)));
double maxy = Max(l[i].y1, Max(l[i].y2, Max(l[i-1].y1, l[i-1].y2)));
if(l[i].xielv > 0)
{
l[i].x1 = minx;
l[i].y1 = miny;
l[i].x2 = maxx;
l[i].y2 = maxy;
}
else
{
l[i].x1 = minx;
l[i].y1 = maxy;
l[i].x2 = maxx;
l[i].y2 = miny;
}
}
}
printf("%d\n", cnt);
}
int main()
{
// freopen("data.txt", "r", stdin);
while(scanf("%d", &n) && n)
{
Init();
Compute();
}
return 0;
}