首页 > 其他 > 详细

2014辽宁ACM省赛 Distance

时间:2014-05-26 05:57:21      阅读:515      评论:0      收藏:0      [点我收藏+]

问题 E: Distance

时间限制1 Sec  内存限制128 MB
提交48  解决12
[提交][状态][论坛]

题目描述

There is a battle field. It is a square with the side length 100 miles, and unfortunately we have two comrades who get hurt still in the battle field. They are in different positions. You have to save them. Now I give you the positions of them, and you should choose a straight way and drive a car to get them. Of course you should cross the battle field, since it is dangerous, you want to leave it as quickly as you can!

输入

 

There are many test cases. Each test case contains four floating number, indicating the two comrades‘ positions (x1,y1), (x2,y2).

Proceed to the end of file.

输出

you should output the mileage which you drive in the battle field. The result should be accurate up to 2 decimals.

样例输入

1.0 2.0 3.0 4.0 15.0 23.0 46.5 7.0

样例输出

140.01 67.61

提示

The battle field is a square local at (0,0),(0,100),(100,0),(100,100).


一道几何题,求出 边界上的两点 (x0,y0),(xn,yn)就可以了,不过有一些特殊情况得讨论,比如分母为0。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
double distance(double x0,double y0,double xn,double yn)
{
    return sqrt((x0-xn)*(x0-xn)+(y0-yn)*(y0-yn));
}
double f1(double x,double x1,double y1,double x2,double y2)
{
    return (y1-y2)/(x1-x2)*(x-x2)+y2;
}
double f2(double y,double x1,double y1,double x2,double y2)
{
    return (x1-x2)/(y1-y2)*(y-y2)+x2;
}
int main()
{
    double x1,y1;
    double x2,y2;
    while(cin>>x1>>y1>>x2>>y2)
    {
        double x0,y0,xn,yn;
        if(y1==y2||x1==x2)
        {
            printf("100.00\n");
            continue;
        }
        x0=0;
        xn=100;
        y0=f1(x0,x1,y1,x2,y2);
        yn=f1(xn,x1,y1,x2,y2);
      //  cout<<y0<<" "<<yn<<endl;
        if(yn>100&&y0>=0&&y0<=100)
        {
            yn=100;
            xn=f2(yn,x1,y1,x2,y2);
        }
        else if(yn>100&&y0<0)
        {
            yn=100;
            xn=f2(yn,x1,y1,x2,y2);
            y0=0;
            x0=f2(y0,x1,y1,x2,y2);
        }
        else if(y0<0&&yn>=0&&yn<=100)
        {
            y0=0;
            x0=f2(y0,x1,y1,x2,y2);
        }
        else if(y0>100&&yn>=0&&yn<=100)
        {
            y0=100;
            x0=f2(y0,x1,y1,x2,y2);
        }
        else if(y0>100&&yn<0)
        {
            y0=100;
            x0=f2(y0,x1,y1,x2,y2);
            yn=0;
            xn=f2(yn,x1,y1,x2,y2);
        }
        else if(y0>=0&&y0<=100&&yn<0)
        {
            yn=0;
            xn=f2(yn,x1,y1,x2,y2);
        }
        printf("%.2lf\n",distance(x0,y0,xn,yn));
    }
    return 0;
}


2014辽宁ACM省赛 Distance,布布扣,bubuko.com

2014辽宁ACM省赛 Distance

原文:http://blog.csdn.net/asdfghjkl1993/article/details/26630267

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!