POJ 3174 Alignment of the Planets

来源:互联网 发布:做矢量图的软件 编辑:程序博客网 时间:2024/04/29 19:48

Alignment of the Planets

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1861 Accepted: 905
Description

Actually, this problem is about alignment of N (1 <= N <= 770) cows numbered 1..N who are grazing in their field that is about 15,000x15,000 units. Their grazing locations all fall on integer coordinates in a standard x,y scheme (coordinates are in the range 0..15,000).

Bessie looks up and notices that she is exactly lined up with Sara and Julie. She wonders how many groups of three aligned cows exist within the field.

Given the locations of all the cows (no two cows occupy the same location), figure out all sets of three cows are exactly collinear. Keep track of the sets, sorting the cows 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 cow i’s location with two space-separated integers that are her x and y coordinates

Output

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

Lines 2..?: Each line contains three space-separated integers that are the cow ID numbers of three collinear cows. 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

Sample Output

1
1 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:

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

. . . . * . *   * . * . . . .   . . . . * . .   . * . . . . .   . . . . . * .  * . . . . . .

The digits mark the collinear cow IDs:

. . . . * . *   * . 4 . . . .   . . . . * . .   . 3 . . . . .   . . . . . * .  1 . . . . . .

题意是说在一个平面内所有给定n个点,求这n个点中三点共线的个数,并输出他们的序号
题目很好理解,就是求斜率判断是否三点共线,但是求斜率这里要用到将出发转化为乘法2,否则无论怎么提交都是错误,即使我测试了将斜率转化为浮点数。
(y1-y2)/(x1-x2)=(y3-y2)/(x3-x2)这是判断斜率相等
转化为乘法则为(y1-y2)* (x3-x2)=(y3-y2)*(x3-x2)

AC

#include <cstdio>int main(){    int n;    int x[15010],y[15010];    scanf("%d",&n);    for(int i=1; i<=n; i++)        scanf("%d%d",&x[i],&y[i]);    int cnt=0;int a[n],b[n],c[n];    for(int i=1; i<=n; i++) //x[i]  y[i]    {        for(int j=i+1; j<=n; j++) //x[j] y[j]        {            for(int t=j+1; t<=n; t++) //x[t] y[t]            {                if((y[t]-y[j])*(x[j]-x[i])==(y[j]-y[i])*(x[t]-x[j]))                {                    a[cnt]=i;                    b[cnt]=j;                    c[cnt]=t;                    cnt++;                }            }        }    }    printf("%d\n",cnt);    for(int i=0;i<cnt;i++)        printf("%d %d %d\n",a[i],b[i],c[i]);    return 0;}
0 0
原创粉丝点击