您的位置:首页 > 产品设计 > UI/UE

Central Avenue Road+csuoj+水题

2014-10-05 16:56 330 查看

Central Avenue Road

Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 30 Solved: 10[Submit][Status][WebBoard]

Description

There are a lot of trees in the park of Jilin University. We want to open up a road in this forest in a straight line from one tree to another. In order to view more scenery, the difference of both sides of the road is minimum (zero or one). If other tree isjust on the road, it is not counted on both sides. Of course, we have a lot of ways to build this road. The best way is that the distance of two reference trees is shortest.

Input

The first line of each case is the number of trees N (2 <= N <=100). The next N lines have two integers that is the x and y coordinates of the i-th tree. The input file is terminated by N=0.

Output

For each case, output the distance of the two reference trees that the road obeys the above rules and the distance is shortest. Keep three digits after decimal point.

Sample Input

4
0 0
3 4
10 0
0 10
0

Sample Output

5.000
解决方案:求建一条两颗树之间的路,要求左边的树的数目和右边的数目之差最多不能超过1。暴力即可,但要特判斜率为0和为无穷的情况。
code:
#include <iostream>#include<cstdio>#include<cmath>#include<cstring>using namespace std;const int maxn=105;int x[maxn],y[maxn];int n;double dist(int i,int j){return  sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+0.000001);}bool judge(int i,int j){int a=y[j]-y[i];int b=x[i]-x[j];int c=x[j]*y[i]-x[i]*y[j];int u=x[i]-x[j];int d=y[i]-y[j];int lf=0,rt=0;if(u==0&&d!=0){for(int k=0; k<n; k++){if(x[k]<x[i]){lf++;}if(x[k]>x[i]){rt++;}}}else if(d==0&&u!=0){for(int k=0; k<n; k++){if(y[k]<y[i]){lf++;}if(y[k]>y[i]){rt++;}}}else{for(int k=0; k<n; k++){if(a*x[k]+b*y[k]+c>0){lf++;}else if(a*x[k]+b*y[k]+c<0){rt++;}}}if(fabs(rt-lf)<=1) return true;else return false;}int main(){while(~scanf("%d",&n)&&n){for(int i=0; i<n; i++){scanf("%d%d",&x[i],&y[i]);}double Max=0x3f3f3f3f3f;for(int i=0;i<n;i++){for(int j=i+1;j<n;j++){if(judge(i,j)){double temp=dist(i,j);if(temp<Max)Max=temp;}}}printf("%.3lf\n",Max);}return 0;}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: