CSU 2088 Pigs can't take a sudden turn(计算几何)

                                   Pigs can't take a sudden turn

题意:求两个随速度变化的点的最小距离(x1 + u1 * t, y1+ v1 * t),(x2 + u2 * t, y2 + v1 * t)。

思路:化简两点间的距离公式得到d^2关于t的二元一次方程:

d ^ 2 = ((u2 - u1)^ 2 + (v2 - v1) ^ 2) * t^2 + ((x2 - x1) * (u2 - u1) + (y2 - y1) * (v2 - v1))2t + (x2 - x1)^2 + (y2 - y1) ^ 2。由此得到二元方程的a, b, c。

(y1好像是csu系统里面定义过, 所以#define重新定义一下)

AC Code:

#include<iostream>
#include<sstream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<map>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<list>
#define mod 1000000007
#define INF 0x3f3f3f3f
#define y1 AC
#define Min 0xc0c0c0c0
#define mst(a) memset(a,0,sizeof(a))
#define f(i,a,b) for(int i = a; i < b; i++)
using namespace std;
const double pi = acos(-1);
const int MAX_N = 1e5 + 5;
const double eps = 1e-8;
typedef long long ll;
double a, b, c;
double x1, y1, u1, v1;
double x2, y2, u2, v2;
double get_ans(double t){
    double ans = a * t * t + b * t + c;
    return sqrt(ans);
}
int main(){
    //freopen("c1.txt", "w", stdin);
    //freopen("c2.txt", "r", stdout);
    //ios::sync_with_stdio(false);
    int T, cas = 1;
    scanf("%d", &T);
    while(T--){
        scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
        scanf("%lf%lf%lf%lf", &u1, &v1, &u2, &v2);
        a = (u2 - u1) * (u2 - u1) + (v2 - v1) * (v2 - v1);
        b = 2 * ((x2 - x1) * (u2 - u1) + (y2 - y1) * (v2 - v1));
        c = (x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1);
        printf("Case %d: ", cas++);
        if(fabs(a) < eps){
            printf("%.6lf\n", get_ans(0));
        }
        else if(-b / (2 *a) < 0){
            printf("%.6lf\n", get_ans(0));
        }
        else{
            printf("%.6lf\n", get_ans(-b / (a * 2)));
        }
    }
    //fclose(stdin);
    //fclose(stdout);
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值