Mom and dad have a problem – their child John never puts his toys away when he is finished playing with them. They gave John a rectangular box to put his toys in, but John is rebellious and obeys his parents by simply throwing his toys into the box. All the toys get mixed up, and it is impossible for John to find his favorite toys.
John’s parents came up with the following idea. They put cardboard partitions into the box. Even if John keeps throwing his toys into the box, at least toys that get thrown into different bins stay separated. The following diagram shows a top view of an example toy box.
For this problem, you are asked to determine how many toys fall into each partition as John throws them into the toy box.
Input
Output
Sample Input
5 6 0 10 60 0 3 1 4 3 6 8 10 10 15 30 1 5 2 1 2 8 5 5 40 10 7 9 4 10 0 10 100 0 20 20 40 40 60 60 80 80 5 10 15 10 25 10 35 10 45 10 55 10 65 10 75 10 85 10 95 10 0
Sample Output
0: 2 1: 1 2: 1 3: 1 4: 0 5: 1 0: 2 1: 2 2: 2 3: 2 4: 2
Hint
题目的大意是给你一个箱子,箱子分成了许多的个子,然后给你一些坐标,判断每个格子里面有几个坐标。这个题我是用的暴力的,也就是一个一个的求,时间有点慢啊。
大概的方法是这样的,将这个坐标点,与每个盒子的四条边分别作叉积,判断出都在左手边的话,那么这个点就是在这个盒子里面了,思路比较简单,代码很长:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
struct point
{
int x,y;
};
struct segment
{
int x1,x2,y1,y2;
};
segment s[10000];
point toy[10000];
int sum[10000];
//我比较喜欢定义大一点的数组
int det(point t,point a,point b)
{
return (b.x-a.x)*(t.y-a.y)-(t.x-a.x)*(b.y-a.y);//做叉积
}
int judge(point t,segment seg1,segment seg2)
{
point a,b,c,d;
a.x=seg1.x1;a.y=seg1.y1;//逆时针,第一个点
b.x=seg1.x2;b.y=seg1.y2;//第二个点
c.x=seg2.x2;c.y=seg2.y2;//第三个点
d.x=seg2.x1;d.y=seg2.y1;//第四个点
if(det(t,a,b)>=0&&det(t,b,c)>=0&&det(t,c,d)>=0&&det(t,d,a)>=0)
//全部满足在左边,则证明这个点在盒子里面
{
return 1;
}
return 0;
}
int main()
{
int n,m,x,y,x1,y1,x2,y2,a,b;
int i,j;
//freopen("in.txt","r",stdin);
while(scanf("%d",&n)&&n)
{
memset(sum,0,sizeof(sum));
scanf("%d%d%d%d%d",&m,&x1,&y1,&x2,&y2);
s[0].x1=x1;
s[0].y1=y1;
s[0].x2=x1;
s[0].y2=y2;//将最左边的两个点存起来
for(i=1;i<=n;i++)
{
scanf("%d%d",&a,&b);
s[i].x1=a;
s[i].y1=y1;
s[i].x2=b;
s[i].y2=y2;//把分隔栏的点都存起来
}
s[i].x1=x2;
s[i].y1=y1;
s[i].x2=x2;
s[i].y2=y2;//把最右边的点都存起来。
/*for(i=0;i<=n+1;i++)
{
printf("%d %d %d %d ",s[i].x1,s[i].y1,s[i].x2,s[i].y2);
}*/
for(j=0;j<m;j++)
{
scanf("%d%d",&toy[j].x,&toy[j].y);
}
int judge_ok;
for(i=0;i<m;i++)
{
judge_ok=0;
for(j=0;j<=n;j++)
{
judge_ok=judge(toy[i],s[j],s[j+1]);//判断在不在格子里面
if(judge_ok)
{
sum[j]++;
break;
}
}
}
for(i=0;i<=n;i++)
{
printf("%d: %d\n",i,sum[i]);
}
printf("\n");
//system("pause");
}
return 0;
}