Language:
Ants
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 |
题意:给n只蚂蚁和该蚂蚁对应的位置以及蚂蚁家的位置每只蚂蚁回家的路不能交叉求每只蚂蚁对应家的位置。
解题思路:判断线段是否交叉如果交叉则交换两只蚂蚁家的位置。依次枚举直至没有交叉。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>
#define inf 0x3f3f3f3f
#define eps 1e-8
using namespace std;
double MAX(double a,double b){
return a>b?a:b;
}
double MIN(double a,double b){
return a<b?a:b;
}
struct point{
double x,y;
};
struct line{
point a,b;
int to;
}A[110];
bool judge(int a,int b){
if(MIN(A[a].a.x,A[a].b.x)>MAX(A[b].a.x,A[b].b.x)||MIN(A[a].a.y,A[a].b.y)>MAX(A[b].a.y,A[b].b.y)||MIN(A[b].a.x,A[b].b.x)>MAX(A[a].a.x,A[a].b.x)||MIN(A[b].a.y,A[b].b.y)>MAX(A[a].a.y,A[a].b.y))
return false;
double h,i,j,k;
h=(A[a].b.x-A[a].a.x)*(A[b].a.y-A[a].a.y)-(A[a].b.y-A[a].a.y)*(A[b].a.x-A[a].a.x);
i=(A[a].b.x-A[a].a.x)*(A[b].b.y-A[a].a.y)-(A[a].b.y-A[a].a.y)*(A[b].b.x-A[a].a.x);
j=(A[b].b.x-A[b].a.x)*(A[a].a.y-A[b].a.y)-(A[b].b.y-A[b].a.y)*(A[a].a.x-A[b].a.x);
k=(A[b].b.x-A[b].a.x)*(A[a].b.y-A[b].a.y)-(A[b].b.y-A[b].a.y)*(A[a].b.x-A[b].a.x);
return h*i<=eps&&j*k<=eps;
}
int main()
{
int n,i,j,k;
while(scanf("%d",&n)!=EOF){
for(i=0;i<n;++i){
scanf("%lf%lf",&A[i].a.x,&A[i].a.y);
}
for(i=0;i<n;++i){
scanf("%lf%lf",&A[i].b.x,&A[i].b.y);
A[i].to=i;
}
while(1){
int flag=1;
for(i=0;i<n;++i){
for(j=i+1;j<n;++j){
if(judge(i,j)){
point temp=A[i].b;A[i].b=A[j].b;A[j].b=temp;
int tmp=A[i].to;A[i].to=A[j].to;A[j].to=tmp;
flag=0;
}
}
}
if(flag)break;
}
for(i=0;i<n;++i){
printf("%d\n",A[i].to+1);
}
}
return 0;
}