poj 3565 Ants(计算几何调整思想)

Ants
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 3855 Accepted: 1172 Special Judge

Description

Young naturalist Bill studies ants in school. His ants feed on plant-louses that live on apple trees. Each ant colony needs its own apple tree to feed itself.

Bill has a map with coordinates of n ant colonies and n apple trees. He knows that ants travel from their colony to their feeding places and back using chemically tagged routes. The routes cannot intersect each other or ants will get confused and get to the wrong colony or tree, thus spurring a war between colonies.

Bill would like to connect each ant colony to a single apple tree so that all n routes are non-intersecting straight lines. In this problem such connection is always possible. Your task is to write a program that finds such connection.

On this picture ant colonies are denoted by empty circles and apple trees are denoted by filled circles. One possible connection is denoted by lines.

Input

The first line of the input file contains a single integer number n (1 ≤ n ≤ 100) — the number of ant colonies and apple trees. It is followed by n lines describing n ant colonies, followed by n lines describing n apple trees. Each ant colony and apple tree is described by a pair of integer coordinates x and y (− 10 000 ≤ x, y ≤ 10 000 ) on a Cartesian plane. All ant colonies and apple trees occupy distinct points on a plane. No three points are on the same line.

Output

Write to the output file n lines with one integer number on each line. The number written on i-th line denotes the number (from 1 to n) of the apple tree that is connected to the i-th ant colony.

Sample Input

5
-42 58
44 86
7 28
99 34
-13 -59
-47 -44
86 74
68 -75
-68 60
99 -60

Sample Output

4
2
1
5
3

Source


题意:求所有直线不想交的配对情况
题解:假设一开始下标相同的都配对,然后不断进行冒泡排序一样的,若相交则交换配对,直到不相交为止
参考:http://hi.baidu.com/novosbirsk/item/16507523e6d53f0d76272c10

#include<stdio.h>
#define N 101
struct point{
    double x,y;
}p1[N],p2[N];
int match[N];
double MIN(double x,double y){ return x<y?x:y; }
double MAX(double x,double y){ return x>y?x:y; }
double cross(struct point p1,struct point p2,struct point p3)
{
    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
int intersect(struct point p1,struct point p2,struct point p3,struct point p4)
{
    if(MAX(p1.x,p2.x)<MIN(p3.x,p4.x)) return 0;
    if(MIN(p1.x,p2.x)>MAX(p3.x,p4.x)) return 0;
    if(MAX(p1.y,p2.y)<MIN(p3.y,p4.y)) return 0;
    if(MIN(p1.y,p2.y)>MAX(p3.y,p4.y)) return 0;
    if(cross(p1,p2,p3)*cross(p1,p2,p4)>0) return 0;
    if(cross(p3,p4,p1)*cross(p3,p4,p2)>0) return 0;
    return 1;
}
int main()
{
    int i,j,n,flag,temp;

    while(scanf("%d",&n)>0)
    {
        for(i=0;i<n;i++) scanf("%lf%lf",&p1[i].x,&p1[i].y);
        for(i=0;i<n;i++) scanf("%lf%lf",&p2[i].x,&p2[i].y);
        for(flag=i=0;i<n;i++) match[i]=i;
        while(!flag)
        {
            for(flag=1,i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    if(i==j) continue;
                    if(intersect(p1[i],p2[match[i]],p1[j],p2[match[j]]))
                    {
                        temp=match[i],match[i]=match[j],match[j]=temp;
                        flag=0;
                    }
                }
            }
        }
        for(i=0;i<n;i++) printf("%d\n",match[i]+1);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值