CSU2088: Pigs can't take a sudden turn

Description

You maybe have ACed a question about computational geometry,which describes two dogs’ journey and calculate the shortest distance between them. Now, I provide a easier problem about two pigs. heir path are half-lines. When they start running, they won’t stop. Why? Because they can’t take a sudden turn.

So, I provide the start points of two pigs and their velocities. Please tell me the shortest distance between them.

We purpose that two pigs start running at the same time and will not stop because they are running in an vast grasslane without any tree.

If they smash into each other, the answer is zero and ignore the influence.

Input

The first line is an integer T which indicates the number of cases. For each case, first line is four numbers x1,y1,x2,y2, indicate the start points of two pigs(x1,y1), (x2,y2). The second line is four numbers u1,v1,u2,v2, indicate the velocities of two pigs(u1,v1),(u2,v2). T <= 1000 The absolute value of x1,y1,x2,y2,u1,v1,u2,v2 will not lager than 1000.

Output

For each case, you should output one line like ”Case i: d”. i stands for the case number and d stands for the answer, which should rounded to 6 decimal places.

Sample Input

5
1 1 2 2
1 1 2 2

1 1 2 2
1 1 -1 -1

1 1 2 2
0 1 0 -1

1 1 1 1
1 1 2 2

0 0 0 1
0 1 1 0

Sample Output

Case 1: 1.414214
Case 2: 0.000000
Case 3: 1.000000
Case 4: 0.000000
Case 5: 0.707107

这题比较简单了,就是直接利用一个二次函数要注意分下类就ok了。

附ac代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
using namespace std;
const double eps=1e-8;
double a,b,c;
double x1,y1,u1,v1;
double x2,y2,u2,v2;
double get(double t)
{
    double ans=a*t*t+b*t+c;
    return sqrt(ans);
}
int main()
{
    int T,cnt=1;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
        scanf("%lf%lf%lf%lf",&u1,&v1,&u2,&v2);
        c=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
        b=2*(x1-x2)*(u1-u2)+2*(y1-y2)*(v1-v2);
        a=(u1-u2)*(u1-u2)+(v1-v2)*(v1-v2);
        printf("Case %d: ",cnt++);
        if(fabs(a)<eps)
        {
            printf("%.6f\n",get(0));
            continue;
        }
        if(-b/(a*2)<0)
        {
            printf("%.6f\n",get(0));
        }
        else
        {
            printf("%.6f\n",get(-b/(a*2)));
        }
    }
    return 0;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值