Collision

Time Limit: 15000/15000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 1623    Accepted Submission(s): 435

Problem Description
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 ≤ 105)

The next line contains four integers x1, y1, x2, y2. The initial position of the two balls is (x1, y1) and (x2, y2). (0 ≤ x1, x2 ≤ x; 0 ≤ y1, y2 ≤ 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 xc and yc, rounded to one decimal place, which indicate the position where the two balls will first collide.

 
Sample Input
3
10 10
1 1 9 9
10 10
0 5 5 10
10 10
1 0 1 10
 
Sample Output
Case #1:
6.0 6.0
Case #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).

 
Source
 

对我来说这不是水题!!!

  为了避免小数,所有数据*2(后面会体会到的)。

  这样想,分类讨论:

    1.x轴坐标相等,y轴坐标相等:直接输出此点坐标。

    2.只有一个轴坐标不等。

    3.两轴坐标都不等。

  设x1,x2为两点x坐标,x1>x2,第一次相遇时过了tx秒,交会在坐标xp,得到:

    xp = n - (x1 + ta - n),  xp = x2 + ta

  可得

    ta = n - (x1 + x2) / 2

  同理 tb = m - (y1 + y2) / 2

  若x1==x2或y1==y2,直接输出求出的tb或ta处理出的坐标即可。

  否则是第三种情况:

    由于x轴相遇周期是n秒,y轴是m秒,所以实际时间是

      time = n - (x1 + x2) / 2 + n * a,

      time = m - (y1 + y2) / 2 + m * b,

    用Exgcd解出来,但是要保证a>=0,b>=0,并且a最小。

  首先:设ta=n-(x1+x2)/2 , tb=m-(y1+y2)/2 , 问题即变为求解n*a-m*b=(tb-ta), Exgcd形式是n*a+m*b=(n,m)

  设g=(n,m),如果(tb-ta)%g!=0说明无解,现在求出的a,b,可以演化出一堆解,形式如:a+k*(m/g), nb-k*(n/g) 这里k为任意整数,这个方法对原方程成立。

  现在要求解满足n*a-m*b=(tb-ta)的非负最小解,可以直接a=a*(tb-ta)/g,a=a%(m/g),此后a为非负数,最小,且符合题意。

#include <iostream>
#include <cstdio>
using namespace std; long long gcd(long long a, long long b){
return b == 0 ? a : gcd(b, a % b);
}
void ex_gcd(long long a, long long b, long long &u, long long &v){
if(b == 0){
u = 1, v = 0;
return ;
}
ex_gcd(b, a%b, v, u);
v = v - a/b *u;
return ;
} int main(){
long long n, m, x1, y1, x2, y2;
long long t, ca = 0;
cin >> t; while(t--){
ca++;
scanf("%lld%lld%lld%lld%lld%lld", &n, &m, &x1, &y1, &x2, &y2);
printf("Case #%lld:\n", ca);
n *= 2; //都乘以2避免后面ta,tb出现小数
m *= 2;
x1 *= 2;
y1 *= 2;
x2 *= 2;
y2 *= 2;
long long time = 0;
long long ta = n - (x1 + x2)/2;
long long tb = m - (y1 + y2)/2;
if(x1 == x2 && y1 == y2){
time = 0;
}
else if(x1 == x2 && y1 != y2){
time = tb;
}
else if(y1 == y2 && x1 != x2){
time = ta;
}
else{
long long g = gcd(n, m);
if((ta - tb) % g != 0){
time = -1; //不会相撞
}
else{
long long a, b;
// n /= g;
// m /= g;
// (ta - tb) /= g;
ex_gcd(n / g, m / g, a, b);
a = a * (tb - ta) / g;
a = (a % (m / g) + (m / g)) % (m / g); //必须对(m/g)而不是对m,???
time = ta + n * a; //x和y运动时间是一样的
}
}
if(time == -1){
printf("Collision will not happen.\n");
}
else{
x1 = (x1 + time) % (2 * n); //利用周期化为合理范围
y1 = (y1 + time) % (2 * m);
if(x1 > n) x1 = 2 * n - x1; //保证在0~n范围内,可以画图示意一下
if(y1 > m) y1 = 2 * m - y1;
printf("%.1lf %.1lf\n", x1 / 2.0, y1 / 2.0);
}
} return 0;
}

  

6-Collision-hdu5114(小球碰撞)的更多相关文章

  1. HTML5 Canvas彩色小球碰撞运动特效

    脚本简介 HTML5 Canvas彩色小球碰撞运动特效是一款基于canvas加面向对象制作的运动小球动画特效.   效果展示 http://hovertree.com/texiao/html5/39/ ...

  2. js实现多个小球碰撞

    实现思路:小球的移动,是通过改变小球的left和top值来改变,坐标分别为(x,y)当x/y值加到最大,即加到父级的宽度或者高度时,使x值或者y值减小,同理当x值或者y值减到最小时,同样的使x值或者y ...

  3. Canvas+Js制作动量守恒的小球碰撞

    目的:通过js实现小球碰撞并实现动量守恒 canvas我们就不多说了,有用着呢. 我们可以通过canvas画2D图形(圆.方块.三角形等等)3D图形(球体.正方体等待). 当然这只是基础的皮毛而已,c ...

  4. js实现小球碰撞游戏

    效果图:  效果图消失只是截的gif图的问题 源码: <!DOCTYPE html> <html lang="en"> <head> <m ...

  5. 【h5游戏开发】egret引擎p2物理引擎 - 小球碰撞地面搞笑的物理现象

    重力的方向和地面的问题 p2中默认的方向是从上到下,如果重力默认是正数的话,物体放到世界中是会从上面往下面飘的 p2中plane地面默认的方向是y轴的方向,而在p2中y轴的方向默认是从上往下 首先来看 ...

  6. uniapp中用canvas实现小球碰撞的小动画

    uniapp 我就不想喷了,踩了很多坑,把代码贡献出来让大家少踩些坑. 实现的功能: 生成n个球在canvas中运动,相互碰撞后会反弹,反弹后的速度计算我研究过了,可以参考代码直接用 防止球出边框 防 ...

  7. 【鸡年大吉】,不知道写点啥,放个demo(小球碰撞)吧,有兴趣的看看

    最初的想法是仿写win7的泡泡屏保效果,但是对于小球的斜碰问题一直没搞明白(如果你会这个,欢迎留言或者做个demo),所以只是简单处理了碰撞后的速度,有时候会看起来很搞笑~~~funny guy 话不 ...

  8. md5 collision(md5碰撞)之记录一些MD5值

    md5 collision之记录一些MD5值   “Magic Hash”的PHP漏洞可以使得攻击者非法获取用户的账号信息.   漏洞原因: PHP以一种特定的方式处理被哈希的字符串,攻击者可以利用其 ...

  9. Collision (hdu-5114

    题意:你有一个以(0, 0), (x, 0), (0, y), (x, y)为边界点的四边形,给你两个点从(x1, y1), (x2, y2)的点发射,以(1, 1)的速度走,碰到边界会反射,问你最终 ...

随机推荐

  1. JDk和Mevan安装和配置

    一.Mevan安装和配置 1.下载Maven 官方下载地址:http://maven.apache.org/download.html 选择你所希望下载的版本,并保存到常用安装目录.这里以Maven ...

  2. zabbix利用python脚本发送钉钉报警

    #!/usr/bin/python # -*- coding: utf-8 -*- import requests import json import sys import os headers = ...

  3. threading模块创建线程

    什么是线程 (thread) 线程也是一种多任务编程方式,可以使用计算机的多核资源.线程被称为轻量级的进程. 线程特征 *线程计算机多核分配的最小单位 *一个进程可以包含多个线程 *线程也是一个运行的 ...

  4. bootstrap table教程--使用入门基本用法

    笔者在查询bootstrap table资料的时候,看了很多文章,发觉很多文章都写了关于如何使用bootstrap table的例子,当然最好的例子还是官网.但是对于某部分技术人员来说,入门还是不够详 ...

  5. Python 字典(Dictionary) update()方法

    refer to: http://www.runoob.com/python/att-dictionary-update.html

  6. jquery类的创建方式及关键字new的原理

    一.由jQuery创建类引发的问题 在用jQuery选择器时候,可以通过下面两种方式获取元素,并得到一个jQuery对象. var d1 = jQuery('#demo01'); var d2 = n ...

  7. AngularJS 最佳实践

    AngularJS 是一个 Web 应用框架,它实现了前端的 MVC 架构,能让开发人员很方便地实现业务逻辑. 举个栗子,要做到下面的效果,以前可能需要写一连串的 JavaScript 代码绑定 N ...

  8. java 面向对象 — 多态

    注意:如果用父类引用指向子类对象的时候.不可以调用,子类中有但是父类中没有的方法. 抽象 方法没有具体方法,以分号结束.例:public abstract void call(); 1.接口必须要有a ...

  9. Excel VBA 找出选定范围不重复值和重复值

    Sub 找出选定范围内不重复的值() On Error Resume Next Dim d As Object Set d = CreateObject("scripting.diction ...

  10. java.nio.charset.UnsupportedCharsetException: cp0

    使用jython调用python,提示console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0. ...