poj 3656 Ants

来源:互联网 发布:大麦盒子怎么设置网络 编辑:程序博客网 时间:2024/06/02 17:45

POJ 3656 : Ants

Ants
Time Limit: 5000MS Memory Limit: 65536KB 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]

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 xand y (−10 000 ≤ xy ≤ 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 5844 867 2899 34-13 -59-47 -4486 7468 -75-68 6099 -60

Sample Output

42153

[Submit]   [Go Back]   [Status]

/*思路来自:http://hi.baidu.com/novosbirsk/blog/item/fb668cf0f362bec47931aae2.html

*题目大意:有n个蚂蚁,有n棵树,知道他们的坐标,请你将每个蚂蚁匹配到一棵树(蚂蚁

*走到这个树下),而且蚂蚁的路径不能交叉。给出一种合理的方案即可。

*看了一下标程,才发现思路的巧妙。一开始时,我们可以看成蚂蚁和树的配对是乱序的(

*可以假设蚂蚁i配对上树i),我们不断检查当前状态是否满足题目要求,若发现有配对线

*是交叉的,那么我们就对其进行交换,使其不再交叉。这种思路有点像冒泡排序:发现逆

*序,就交换。

*n为100,暴搜应该会超时,但是用冒泡排序的思想进行调整,100ms内能出结果。


使用这种思路敲了一下代码

最大的收获是学习了几个实用的模版

*/

//97ms

#include <cstdio>

#include <algorithm>

using namespace std;

#define N 101

#define eps 1e-8

struct Point

{

    double x, y;


    Point(double x, double y) :

        x(x), y(y)

    {

    }


    Point() :

        x(0), y(0)

    {

    }


};

Point p1[N];//蚂蚁

Point p2[N];//大树

inline int sgn(double x)

{

    return (x < -eps) ? -1 : x > eps;

}


double dotdet(double x1, double y1, double x2, double y2)

{

    return x1 * x2 + y1 * y2;

}


double crossdet(double x1, double y1, double x2, double y2)

{

    return x1 * y2 - x2 * y1;

}


//右手螺旋定则,1:a在cd右侧,-1:a在cd左侧,0:三点共线

int cross(const Point &a, const Point &c, const Point &d)//叉积定理

{

    return sgn(crossdet(a.x - c.x, a.y - c.y, d.x - c.x, d.y - c.y));

}


//在cross(a,c,d)==0的基础上,可判断点a是否在cd内部

//使用点积判断前后的典型例子。若v(CA).v(DA) < 0, 则点A必在CD间

int between(const Point &a, const Point &c, const Point &d)

{

    return sgn(dotdet(c.x - a.x, c.y - a.y, d.x - a.x, d.y - a.y)) != 1;

}


//两线段相交情况:0:不相交,1:规范相交,2:不规范相交(交于端点或重合)

int seg_intersect(const Point &a, const Point &b, const Point &c, const Point&d)

{

    int a_cd = cross(a, c, d);

    if (a_cd == 0 && between(a, c, d)) return 2;

    int b_cd = cross(b, c, d);

    if (b_cd == 0 && between(b, c, d)) return 2;

    int c_ab = cross(c, a, b);

    if (c_ab == 0 && between(c, a, b)) return 2;

    int d_ab = cross(d, a, b);

    if (d_ab == 0 && between(d, a, b)) return 2;

    if ((a_cd ^ b_cd) == -2 && (c_ab ^ d_ab) == -2) return 1;

    return 0;

}


int main()

{

    int n, i, j, ans[N];

    scanf("%d", &n);

    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 (i = 0; i < n; i++)

        ans[i] = i;//原来蚂蚁的位置

    while (1)

    {

        int ok = 1;

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

        {

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

            {

                if (i == j)

                    continue;

                if (seg_intersect(p1[i], p2[ans[i]], p1[j], p2[ans[j]]))//如果相交换

                {

                    swap(ans[i], ans[j]);

                    ok = 0;

                }

            }

        }

        if (ok)break;//只到不能交换为止

    }

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

        printf("%d\n", ans[i]+1);

    return 0;

}

原创粉丝点击