您的位置:首页 > 其它

2014辽宁ACM省赛 Distance

2014-05-22 23:19 190 查看
问题 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: