题目:http://acm.hdu.edu.cn/showproblem.php?pid=3952
任意选取两个水果,并选出水果上两个端点组成一条直线去切割其他的水果,就是判断直线与线段是否相交(n^3*k^3)
代码:
#include<stdio.h>
struct node2
{
int x,y;
};
struct node
{
int num;
node2 w[11];
}s[11];
bool judge(node2 c,node2 d,node2 a,node2 b)//线段端点为a,b,直线上两点c,d
{
node2 tp1,tp2,tp3;
tp1.x=a.x-c.x;
tp1.y=a.y-c.y;//p1-q1
tp2.x=d.x-c.x;
tp2.y=d.y-c.y;//q2-q1
tp3.x=b.x-c.x;
tp3.y=b.y-c.y; //p2-q1
//(p1-q1)x(q2-q1)*(q2-q1)x(p2-q1)>=0
if((tp1.x*tp2.y- tp1.y*tp2.x)*(tp2.x*tp3.y-tp2.y*tp3.x)>=0)
return true;
else
return false;
}
int main()
{
int t,n,i,j,k,p,q,r,ans,max,abc;
while(scanf("%d",&t)!=EOF)
{
abc=0;
while(t--)
{
abc++;
max=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&s[i].num);
for(j=0;j<s[i].num;j++)
scanf("%d%d",&s[i].w[j].x,&s[i].w[j].y);
}
printf("Case %d: ",abc);
if(n==1)
{
printf("1\n");
continue;
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
for(k=0;k<s[i].num;k++)
{
for(p=0;p<s[j].num;p++)
{
ans=2;
for(q=0;q<n;q++)
{
if(q==i||q==j)
continue;
for(r=0;r<s[q].num-1;r++)
{
if(judge(s[i].w[k],s[j].w[p],s[q].w[r],s[q].w[r+1]))
{
ans++;
break;
}
}
}
if(max<ans)
max=ans;
}
}
}
}
printf("%d\n",max);
}
}
return 0;
}
posted on
浙公网安备 33010602011771号