【计算几何/叉积运用】 HRBUST 1069 Bee Movie

Bee Movie

Time Limit: 1000 MS Memory Limit: 65536 K

Description

Barry B. Benson is “just an ordinary bee” in a hive located in Sheep’s Meadow in Central Park in New York City. Barry recently graduated from college and is about to enter the hive’s Honex Industries (a division of Honesco Corporation and owned by the Hexagon Group) honey-making workforce. Along with his best friend Adam Flayman (voiced by Matthew Broderick) Barry is initially very excited, but his latent, non-conformist attitude emerges upon finding out that his choice of job will never change once picked.He absolutely disappointed, he joins the team responsible for bringing the honey and pollination of the flowers to visit the world outside the hive. The bee will draw out in battle array when they want to go outside.

Actually, this problem is about alignment of N (1 ≤ N ≤ 455) bees numbered 1..N who are grazing in their field that is about 15,000×15,000 units. Their grazing locations all fall on integer coordinates in a standard x,y scheme (coordinates are in the range 0..15,000).
Barry looks up and notices that she is exactly lined up with Huacm534(bee) and AcmIcpc20060820322(bee). He wonders how many groups of three aligned bees exist within the field.
Given the locations of all the bees (no two bees occupy the same location), figure out all sets of three bees are exactly collinear. Keep track of the sets, sorting the bees in each set by their ID number, lowest first. Then sort the sets by the three ID numbers (lowest first), breaking ties by examining the second and third ID numbers.

Input

Line 1: A single integer, N

Lines 2..N+1: Line i+1 describes bee i’s location with two space-separated integers that are his x and y coordinates

Output

Line 1: A single integer X that is the number of sets of three bees that are exactly collinear. A set of four collinear bees would, of course, result in four sets of three collinear bees.

Lines 2..X+1: Each line contains three space-separated integers that are the bee ID numbers of three collinear bees. The lines are sorted as specified above. This output section is empty if no collinear sets exist.

Sample Input

8
0 0
0 4
1 2
2 4
4 3
4 5
5 1
6 5
4
0 0
1 1
2 2
3 3

Sample Output

1
1 3 4
4
1 2 3
1 2 4
1 3 4
2 3 4

Hint

Be careful of floating point arithmetic. Floating point comparison for equality almost never works as well as one would hope.

Explanation of the sample 1:

Eight bees grazing on a grid whose lower left corner looks like this:

… . 6 . 8

2 . 4 … .

… . 5 . .

. 3 … . .

… . . 7 .

1 … …

The digits mark the collinear bee IDs:

… . * . *

  • . 4 … .

… . * . .

. 3 … . .

… . . * .

1 … …

Author

kenan5020@Monster

题意

给你n个点,告诉你这些点的坐标,让你求出这当中能在一条直线上的三个点的数量,并输出按照他们的标号从小到大输出这三个点。

思路

简单计算几何
一个叉积的运用
对于任意三点组成的两个向量如果他们的叉积为0,则这三点段共线。
设这三个点分别是ABC
分别求出向量AB,BC的值
向量AB = (x1,y1)
向量BC = (x2,y2)
AB x BC = 0
即 x1y2 - x2y1 = 0;
则AB与BC平行。
又因为他们有公共的顶点B
所以这三点共线。
我们可以先按顺序存下所有的点对
然后我们可以按照顺序for三重循环一下,暴力写出所有三个点的情况,然后如果三点所构成的两个向量叉积为0那么存一下这三个点就好。
最后按照题目意思输出即可。

坑点

AC代码

#include<bits/stdc++.h>
using namespace std;

typedef struct{
    int x,y;

}poi;

typedef struct{
    int a,b,c;
}b;
b ans[10006];
poi a[10006];

/*判断三点是否共线*/
bool judge(poi aa,poi bb,poi cc)
{
    //先求出三点构成的两个向量
    int ax,ay;
    int bx,by;
    ax = aa.x - bb.x;
    ay = aa.y - bb.y;
    bx = aa.x - cc.x;
    by = aa.y - cc.y;
    if(ax * by - ay *bx == 0 ) return true;///叉积公式核心
    return false;
}

void solve(void)
{
    int n;
    std::ios::sync_with_stdio(false);
    while(cin>>n)
    {
        for(int i = 0 ; i < n ; i++)
        {
            cin>>a[i].x>>a[i].y;
        }
        int vis = 0;
        for(int i = 0 ; i < n ; i++)
        {
            for(int j = i + 1 ; j < n ; j++)
            {
                for(int k = j + 1 ; k < n ; k++)
                {
                    if(judge(a[i],a[j],a[k]))
                    {
                        ans[vis].a = i;
                        ans[vis].b = j;
                        ans[vis++].c = k;
                    }
                }
            }
        }
        cout<<vis<<endl;
        for(int i = 0 ; i < vis ; i++)
        {
            cout<<ans[i].a+1<<' '<<ans[i].b+1<<' '<<ans[i].c+1<<endl;
        }
    }
}

int main(void)
{
    solve();
    return 0;
}

猜你喜欢

转载自blog.csdn.net/peng0614/article/details/80428612
Bee
今日推荐