POJ3565-Ants

Ants
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 4251 Accepted: 1309 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  ≤ 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 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

//AC代码
/*
题意:n个蚂蚁和n棵树,给出他们的坐标,现在n个蚂蚁走到这n个树下产生n条路径
问你求出一种方案使之蚂蚁们的路径之间不能交叉(给出一种合理的方案即可)
此题开始我们可以看成蚂蚁和树的配对是乱序的(假设蚂蚁i配对上树j)然后我们不断匹配不断检查当前状态是否满足题目要求
若发现有配对线是交叉的,那么我们就对其进行交换,使其不再交叉,到最后一定可以找出不交叉的路径
*/
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iomanip>
#include<map>
#include<cstdlib>
#include<cmath>
#include<vector>
#define LL long long
#define IT __int64
#define zero(x) fabs(x)<eps
#define mm(a,b) memset(a,b,sizeof(a))
const int INF=0x7fffffff;
const double inf=1e8;
const double eps=1e-10;
const double PI=acos(-1.0);
const int Max=210;
using namespace std;
int sign(double x)
{
    return (x>eps)-(x<-eps);
}
typedef struct Node
{
    int x;
    int y;
    Node(const int &_x=0, const int &_y=0) : x(_x), y(_y) {}
    void input()
    {
        cin>>x>>y;
    }
    void output()
    {
        cout<<x<<" "<<y<<endl;
    }
}point;
point ant[Max],tree[Max];
int res[Max];
double xmult(point p0,point p1,point p2)
{
	return(p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
bool IsIntersected(point s1,point e1,point s2,point e2)
{
	return (max(s1.x,e1.x)>=min(s2.x,e2.x))&& (max(s2.x,e2.x)>=min(s1.x,e1.x))&&(max(s1.y,e1.y)>=min(s2.y,e2.y))&&(max(s2.y,e2.y)>=min(s1.y,e1.y))&&(xmult(s1,s2,e1)*xmult(s1,e1,e2)>=0)&&(xmult(s2,s1,e2)*xmult(s2,e2,e1)>=0);
}
int main()
{
    int n,m,i,j;
    while(cin>>n)
    {
        for(i=1;i<=n;i++)
            ant[i].input();
        for(j=1;j<=n;j++)
            tree[j].input();
        for(i=1;i<=n;i++)
            res[i]=i;
        while(true)
        {
            int ok=1;
            for(i=1;i<=n;i++)
            {
                for(j=1;j<=n;j++)
                {
                    if(i==j)
                        continue;
                    if(IsIntersected(ant[i],tree[res[i]],ant[j],tree[res[j]]))
                    {//不断检查当前状态是否满足题目要求,若发现有配对线是交叉的,那么我们就对其进行交换,使其不再交叉
                        swap(res[i],res[j]);
                        ok=0;
                    }
                }
            }
            if(ok)//如果找出了没有相交线路直接跳出
                break;
        }
        for(i=1;i<=n;i++)
            cout<<res[i]<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值