Collision (hdu-5114)

来源:互联网 发布:ti 知乎 编辑:程序博客网 时间:2024/05/29 16:46
Matt is playing a naive computer game with his deeply loved pure girl. 

The playground is a rectangle with walls around. Two balls are put in different positions inside the rectangle. The balls are so tiny that their volume can be ignored. Initially, two balls will move with velocity (1, 1). When a ball collides with any side of the rectangle, it will rebound without loss of energy. The rebound follows the law of refiection (i.e. the angle at which the ball is incident on the wall equals the angle at which it is reflected). 

After they choose the initial position, Matt wants you to tell him where will the two balls collide for the first time.
Input
The first line contains only one integer T which indicates the number of test cases.

For each test case, the first line contains two integers x and y. The four vertices of the rectangle are (0, 0), (x, 0), (0, y) and (x, y). (1 ≤ x, y ≤ 10 5

The next line contains four integers x 1, y 1, x 2, y 2. The initial position of the two balls is (x 1, y 1) and (x 2, y 2). (0 ≤ x 1, x 2 ≤ x; 0 ≤ y 1, y 2 ≤ y)
Output
For each test case, output “Case #x:” in the first line, where x is the case number (starting from 1). 

In the second line, output “Collision will not happen.” (without quotes) if the collision will never happen. Otherwise, output two real numbers x c and y c, rounded to one decimal place, which indicate the position where the two balls will first collide.
Sample Input
310 101 1 9 910 100 5 5 1010 101 0 1 10
Sample Output
Case #1:6.0 6.0Case #2:Collision will not happen.Case #3:6.0 5.0          
Hint
In first example, two balls move from (1, 1) and (9, 9) both with velocity (1, 1), the ball starts from (9, 9) will rebound at point (10, 10) then move with velocity (−1, −1). The two balls will meet each other at (6, 6).
在一x*y的平面里,两个点(x1y1)(x2y2)以方向(1,1)运动,遇到边界反弹,求出最早在哪点相遇
解题思路:
可以想到相交点只能是正数或是带 .5 所以一开始就将数据都乘以2;
如果两个坐标一开始就相当直接输入就好了
如果这两个坐标有一个相同的,那么这个相同的坐标就会一直相同,以y相同x1>x2为例:
xp=n-(x1+tx-n);xp=x2+tx;
解出tx=n-(x1+x2)/2;
同理如果x相同ty=m-(y1+y2)/2;
我们就可以将tx,ty处理一下就好了
如果是x1=x2
由于x,y轴的相遇周期分为为n和m(这个自己拿笔试试就知道)
所以t=n-(x1+x2)/2+n*a;(设tx=n-(x1+x2)/2)
同理t=m-(y1+y2)/2+m*b(ty=m-(y1+y2)/2)
方程联立得na-mb=ty-tx;这个就可以用Exgcd
求出最小解,处理即可,如果无解,说明不会相交
#include <iostream>#include <cstdio>using namespace std;typedef long long ll;ll  gcd(ll x,ll y){    if(y==0)        return x;    else        return gcd(y,x%y);}void Exgcd(ll a,ll b,ll &x,ll &y){    if(b==0)    {        x=1;        y=0;        return ;    }    ll x1,y1;    Exgcd(b,a%b,x1,y1);    x=y1;    y=x1-(a/b)*y1;}int main(){    int T,cas=0;    scanf("%d",&T);    while(T--)    {        cas++;        ll n,m,x1,x2,y1,y2,x,y;        scanf("%lld%lld",&n,&m);        scanf("%lld%lld%lld%lld",&x1,&y1,&x2,&y2);        n*=2;m*=2;x1*=2;y1*=2;x2*=2;y2*=2;        ll tx=n-(x1+x2)/2;        ll ty=m-(y1+y2)/2;        ll tim=-1;        if(x1==x2&&y1==y2)            tim=0;        if(x1!=x2&&y1==y2)            tim=tx;        if(x1==x2&&y1!=y2)            tim=ty;        if(x1!=x2&&y1!=y2)        {            ll d=gcd(n,m);            Exgcd(n,m,x,y);            if((ty-tx)%d==0)            {                x=(ty-tx)/d*x;                x=(x%(m/d)+m/d)%(m/d);//这行和上行是求出最小x                tim=tx+x*n;            }        }        printf("Case #%d:\n",cas);        if(tim==-1)            printf("Collision will not happen.\n");        else        {            x1=(x1+tim)%(2*n);            y1=(y1+tim)%(2*m);            if(x1>n)                x1=2*n-x1;            if(y1>m)                y1=2*m-y1;//因为包括n和m所以这么算会算上这两个点            printf("%.1lf %.1lf\n",x1/2.0,y1/2.0);        }    }    return 0;}
原创粉丝点击