您的位置:首页 > 其它

Poj-2398-Toy Storage

2015-11-08 10:21 211 查看
Description

Mom and dad have a problem: their child, Reza, never puts his toys away when he is finished playing with them. They gave Reza a rectangular box to put his toys in. Unfortunately, Reza 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 Reza to find his favorite toys anymore. 

Reza's parents came up with the following idea. They put cardboard partitions into the box. Even if Reza keeps throwing his toys into the box, at least toys that get thrown into different partitions stay separate. The box looks like this from the top: 



We want for each positive integer t, such that there exists a partition with t toys, determine how many partitions have t, toys.

Input

The input consists of a number of cases. The first line consists of six integers n, m, x1, y1, x2, y2. The number of cardboards to form the partitions is n (0 < n <= 1000) and the number of toys is given in m (0 < m <= 1000). The coordinates of the upper-left
corner and the lower-right corner of the box are (x1, y1) and (x2, y2), respectively. The following n lines each consists of two integers Ui Li, indicating that the ends of the ith cardboard is at the coordinates (Ui, y1) and (Li, y2). You may assume that
the cardboards do not intersect with each other. The next m lines each consists of two integers Xi Yi specifying where the ith toy has landed in the box. You may assume that no toy will land on a cardboard. A line consisting of a single 0 terminates the input.

Output

For each box, first provide a header stating "Box" on a line of its own. After that, there will be one line of output per count (t > 0) of toys in a partition. The value t will be followed by a colon and a space, followed the number of partitions containing
t toys. Output will be sorted in ascending order of t for each box.

Sample Input

4 10 0 10 100 0

20 20

80 80

60 60

40 40

5 10

15 10

95 10

25 10

65 10

75 10

35 10

45 10

55 10

85 10

5 6 0 10 60 0

4 3

15 30

3 1

6 8

10 10

2 1

2 8

1 5

5 5

40 10

7 9
0

Sample Output

Box

2: 5

Box

1: 4

2: 1

这道题的大概意思是先输入6个数字:n,m,x1,y1,x2,y2。n代表卡片的数量,卡片竖直(或倾斜)放置在盒内,可把盒子分为n+1块区域,然后分别用0到n表示,m代表玩具的个数,(x1,y1)代表盒子的左上顶点坐标,(x2,y2)代表盒子的右下顶点坐标,接下来的n行,每行都有两个数a,b,(a,y1),(b,y2)分别代表卡片的两个顶点位置,接下来的m行每行两个数从c,d,(c,d),代表玩具放置的坐标,最后让你输出当区域内玩具的个数(递增,0除外),以及有几个同样数目玩具的区域,当输入第一个数为0时结束。

简化一下就是给你一个长方形平面左上顶点和右下顶点的坐标,然后有n条线,将长方形分成n+1块,然后给你m个点的坐标,问你区域内有几个点(递增),有多少个有相同点数的区域

思路:这道题和Poj-2318-TOYS的题意差不多,思路也基本相同,只要想到用叉乘判断点是否在区域内就可以了。

叉乘的性质: 若向量P,Q

P*Q<0;  则Q在P的右边;

P*Q=0;则Q与P平行或重合

P*Q>0;则Q在P的左边

因为此题中玩具的点已经可以确定在另外三边中,即左边,上边,下边,所以只需判断是否在由卡片组成的边左边即可,我们对每一个点都从第一条边判断开始,直到找到其所属区域,然后计数,统计,输出,注意题中卡牌代表的边的坐标不是从左到右顺序给出的,因此要进行排序。

代码如下:

#include <algorithm>

#include <iostream>

#include <stdio.h>

#include <string.h>

#include <math.h>

using namespace std;

struct note{

    int x1;

    int x2;

};

struct note a[5005],b;

int c[5005];

//叉乘计算

int chacheng(int b1,int b2,int b3,int b4)

{

    int s=b1*b4-b2*b3;

    if(s>0)

        return 1;

    else

        return 0;

}

int main()

{

    int n;

    while(~scanf("%d",&n)&&n!=0)

    {

        memset(c,0,sizeof(c));

        int m,x1,y1,x2,y2,i,j;

        scanf("%d %d %d %d %d",&m,&x1,&y1,&x2,&y2);

        for(int i=1;i<=n;i++)

            scanf("%d %d",&a[i].x1,&a[i].x2);

        //用冒泡对边进行从左到右排序

        for(i=1;i<n;i++)

            for(j=1;j<n+1-i;j++)

            {

                if(a[j].x2>a[j+1].x2)

                {

                    b.x1=a[j].x1;

                    b.x2=a[j].x2;

                    a[j].x1=a[j+1].x1;

                    a[j].x2=a[j+1].x2;

                    a[j+1].x1=b.x1;

                    a[j+1].x2=b.x2;

                }

            }

        //将长方形的右边作为最后一条边

        a[n+1].x1=x2;

        a[n+1].x2=x2;

        for(i=0;i<m;i++)

        {

            int x,y;

            scanf("%d %d",&x,&y);

            for(j=1;j<=n+1;j++)

            {

                //判断此点是否在当前边的左边

                int c1;

                c1=chacheng(a[j].x1-a[j].x2,y1-y2,x-a[j].x2,y-y2);

                if(c1==1)

                {

                    c[j-1]++;

                    break;

                }

            }

        }

        //按区域点数多少进行排序

        sort(c,c+n+1);

        //统计,输出结果

        printf("Box\n");

        int jishu=1;

        for(i=0;i<=n;i++)

        {

            if(c[i]!=0)

            {

                if(c[i]==c[i+1])

                    jishu++;

                else

                {

                    printf("%d: %d\n",c[i],jishu);

                    jishu=1;

                }

            }

        }

    }

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: