Central Avenue Road+csuoj+水题

Central Avenue Road

Time Limit: 3 Sec   Memory Limit: 128 MB
Submit: 30   Solved: 10
[ Submit][ Status][ Web Board]

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 is just 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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值