分析&&题解:
需要注意不少细节,首先需要判断两条线段是否重合,然后考虑起点和终点重合的情况(结果需要减1),然后还是继续套模板。AC代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <algorithm>
#include <cmath>
using namespace std;
int W,H,L;
struct point
{
double x;
double y;
}Point[66];
struct line
{
point s;
point e;
}Line[100];
double multiply(point sp,point ep,point op)
{
return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
}
bool intersect(line u,line v)
{
return( (max(u.s.x, u.e.x)>=min(v.s.x, v.e.x))&& //排斥实验
(max(v.s.x, v.e.x)>=min(u.s.x, u.e.x))&&
(max(u.s.y, u.e.y)>=min(v.s.y, v.e.y))&&
(max(v.s.y, v.e.y)>=min(u.s.y, u.e.y))&&
(multiply(v.s, u.e, u.s)*multiply(u.e, v.e, u.s)>=0)&& //跨立实验
(multiply(u.s, v.e, v.s)*multiply(v.e, u.e, v.s)>=0));
}
int main()
{
while(scanf("%d%d", &W, &H) && (W+H) )
{
cin >> L;
point s,g;
scanf("%lf%lf", &s.x, &s.y);
for(int i=0; i<L; i++)
{ //l+1个点
scanf("%lf%lf",&g.x,&g.y);
Line[i].s=s;
Line[i].e=g;
s=g;
}
int cnt =1;
for(int i=0;i<L;i++)
{
for(int j=i+1;j<L;j++)
{
if (Line[i].e.x==Line[j].s.x && Line[i].e.y==Line[j].s.y ||
Line[i].s.x==Line[j].e.x && Line[i].s.y==Line[j].e.y) continue;
if( intersect(Line[i], Line[j]))
{
cnt++;
//cout << i << " : "<< j << endl;
}
}
}
int ans =cnt+L+1;
if(Point[0].x == Point[L].x && Point[0].y == Point[L].y) ans--;
cout << ans<< endl;
}
}