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. Java 异常Exception e中e的getMessage()和toString()方法的区别

    示例代码1: public class TestInfo { private static String str =null; public static void main(String[] arg ...

  2. 设计一个栈,设计一个max()函数,求当前栈中的最大元素

    #include <iostream> using namespace std; #define MAXSIZE 256 typedef struct stack { int top; i ...

  3. Inno Setup 软件封装

    例子: ; 脚本用 Inno Setup 脚本向导 生成. ; 查阅文档获取创建 INNO SETUP 脚本文件的详细资料! #define MyAppName "隧道照明系统客户端程序&q ...

  4. NOIP模拟赛(洛谷11月月赛)

    T1  终于结束的起点 题解:枚举啊... 斐波那契数 第46个爆int,第92个爆long long.... 发现结果一般是m的几倍左右....不用担心T. #include<iostream ...

  5. vue路由初始化路转

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. GNU Radio: 射频子板

    本文简要介绍 USRP 配套的子板参数信息. 射频子板WBX-40 性能特点 频率覆盖:50 MHz – 2.2GHz 最大信号处理带宽:40MHz 行为描述 WBX-40提供高宽带收发器,可提供高达 ...

  7. SCSI协议

    SCSI是一套完整的数据传输协议,其主要功能是在主机和存储设备之间传送命令.状态和块数据.在各类存储技术中,SCSI技术可谓是最重要的脊梁. SCSI协议位于操作系统和外部资源之间,它具有一系列的功能 ...

  8. springboot的interceptor(拦截器)的应用

    一.SpringMVC 中的Interceptor 拦截器也是相当重要和相当有用的,它的主要作用是拦截用户的请求并进行相应的处理.在web开发中,拦截器是经常用到的功能.它可以帮我们验证是否登陆.预先 ...

  9. JQuery input file 上传图片

    表单元素file设置隐藏,通过其他元素打开: .imgfile为input file $(".ul").click(function () {return $(".img ...

  10. 安装配置Android开发环境SDK

    引言: 好搞事情,搞点移动端测试高大尚的东西,首先先得把环境搭建起来: 1.下载 握了个草,很多网站都直接推荐到android官网去下载,叔不知google官网早就被我大天朝给墙了,对于不喜欢FQ的天 ...