zoj 3728 Collision(2013亞洲區域賽 長沙站 C)


http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5074

Collision

Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge

There's a round medal fixed on an ideal smooth table, Fancy is trying to throw some coins and make them slip towards the medal to collide. There's also a round range which shares exact the same center as the round medal, and radius of the medal is strictly less than radius of the round range. Since that the round medal is fixed and the coin is a piece of solid metal, we can assume that energy of the coin will not lose, the coin will collide and then moving as reflect.

Now assume that the center of the round medal and the round range is origin ( Namely (0, 0) ) and the coin's initial position is strictly outside the round range. Given radius of the medal Rm, radius of coin r, radius of the round range R, initial position (xy) and initial speed vector (vxvy) of the coin, please calculate the total time that any part of the coin is inside the round range.

Please note that the coin might not even touch the medal or slip through the round range.



思路:題意很容易理解就直接說思路了,我是解方程算的,設過t時間硬幣進入R區域,那麼有

x'=x+t*vx

y'=y+t*vy

帶入到方程: (x')^2+(y')^2<=(R+r)^2,解出t1,t2,只有當t1!=t2且t1,t2均大於0時才表示硬幣進入到了R,否則直接輸出0即可(無解或只有一解或有解小於0),然後用相同的方法判斷硬幣是否進入Rm,如果沒有碰到,則|t1-t2|就是答案,否則計算出硬幣碰到Rm的時間t3,設t1>t2,那麼2*(t3-t2)即爲答案(因爲硬幣碰到Rm後做反射運動)。

代碼如下:


#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <cmath>
#define maxn 100100
using namespace std;
double Rm,R,r,x,y,vx,vy;
int main()
{
  //  freopen("dd.txt","r",stdin);
    while(scanf("%lf%lf%lf%lf%lf%lf%lf",&Rm,&R,&r,&x,&y,&vx,&vy)!=EOF)
    {
        double a=vx*vx+vy*vy;
        double b=2*(vx*x+vy*y);
        double c=x*x+y*y-(R+r)*(R+r);
        double deta=b*b-4*a*c;
        if(deta<=0||a==0)
        {
            printf("0.000\n");
            continue;
        }
        double t1=(-b+sqrt(deta))/(2*a),t2=(-b-sqrt(deta))/(2*a);
        if(t2>=0)//和R有交點
        {
            c=x*x+y*y-(Rm+r)*(Rm+r);
            double deta1=b*b-4*a*c;
            double time;
            if(deta1<=0)//沒有碰到medal
            {
                 time=t1-t2;
            }
            else
            {
                double t3=(-b-sqrt(deta1))/(2*a);
                time=2*(t3-t2);
            }
            printf("%.3lf\n",time);
        }
        else
        {
            printf("0.000\n");
        }
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章