链接:http://poj.org/problem?id=2318
这道题是比较简单的计算几何问题,只要找每个点应该在第几块内部就可以了,用叉积判断方向,二分查找
View Code
1 #include<stdio.h> 2 #include<string.h> 3 struct point 4 { 5 double x,y; 6 }; 7 struct 8 point a[10005]; 9 int num[5005]; 10 double mul(point p1,point p2,point p3)//叉积 11 { 12 return (p2.x-p1.x)*(p3.y-p2.y)-(p3.x-p2.x)*(p2.y-p1.y); 13 } 14 int main() 15 { 16 int n,m; 17 point t1,t2; 18 int flage=0; 19 while(scanf("%d",&n)&&n) 20 { 21 if(flage) 22 printf("\n"); 23 flage=1; 24 memset(num,0,sizeof(num)); 25 scanf("%d%lf%lf%lf%lf",&m,&t1.x,&t1.y,&t2.x,&t2.y); 26 a[0].x=t1.x; 27 a[0].y=t1.y; 28 a[1].x=t1.x; 29 a[1].y=t2.y; 30 for(int i=2;i<=2*n;i+=2) 31 { 32 double x1,x2; 33 scanf("%lf%lf",&x1,&x2); 34 a[i].x=x1; 35 a[i+1].x=x2; 36 a[i].y=t1.y; 37 a[i+1].y=t2.y; 38 } 39 a[2*n+2].x=t2.x; 40 a[2*n+2].y=t1.y; 41 a[2*n+3].x=t2.x; 42 a[2*n+3].y=t2.y; 43 point p1; 44 for(int j=0;j<m;j++) 45 { 46 scanf("%lf%lf",&p1.x,&p1.y); 47 int low=0; 48 int high=n; 49 int mid=(low+high)/2; 50 while(mul(p1,a[2*mid+1],a[2*mid])*mul(p1,a[2*mid+3],a[2*mid+2])>0)//二分查找 51 { 52 if(mul(p1,a[2*mid+1],a[2*mid])>0) 53 { 54 high=mid-1; 55 mid=(low+high)/2; 56 } 57 else 58 { 59 low=mid+1; 60 mid=(low+high)/2; 61 } 62 } 63 num[mid]++; 64 } 65 for(int i=0;i<=n;i++) 66 { 67 printf("%d: %d\n",i,num[i]); 68 } 69 } 70 return 0; 71 }