LightOJ - 1323
2018-02-11 20:29
281 查看
You are given a rectangular billiard board, L and W be the length and width of the board respectively. Unlike other billiard boards it doesn't have any pockets. So, the balls move freely in the board. Assume that initially some balls are in the board and all of them are moving diagonally. Their velocities are same but their direction may be different. When one or more balls bounce off, their position changes but their velocity remains same.
You are given the initial positions of the balls and their directions; your task is to find the position of the balls after K seconds. The board is placed in the 2D plane such that the boundaries are (0, 0), (L, 0), (L, W) and (0, W). The positions of balls are given as 2D co-ordinates and they all lie inside the board. And the directions are given as one of the following {NE, SE, SW, NW}, N, E, S and W denote North, East, South and West respectively. NE means North-East so both x and y are increasing. The balls are so small that their radiuses can be said to be 0. In each second, the balls advance one unit in their direction. Here one unit doesn't mean Euclidean one unit. For example, if the current position of a ball is (x, y) and its direction is NW then in the next second its position will be (x-1, y+1).
When two or more balls bounce off, they may change their directions as shown in the pictures. You can rotate the pictures to get all possible results. Remember that the balls may bounce at non-integer points.
Bouncing on walls
Bounce result between two balls
Bounce result amongst 3 balls and 4 balls
InputInput starts with an integer T (≤ 25), denoting the number of test cases.
Each case starts with a line containing four integers L, W (5 ≤ W ≤ L ≤ 108), n (1 ≤ n ≤ 1000) and K (1 ≤ K ≤ 108) where n denotes the number of balls in the board. Each of the next n lines contains two integers x y and a string d, where (x, y) (0 ≤ x ≤ L, 0 ≤ y ≤ W) denotes the co-ordinate of the ball and d can be one of {NE, SE, SW, NW} which denotes the direction of the ball respectively. You can safely assume that the balls are placed in different positions initially.
OutputFor each case, print the case number in a single line. Then print the position of the balls. Sort the positions first by their x co-ordinate, and order the ones which have same x coordinate by their y coordinates all in ascending order.
Sample Input2
10 5 6 1
1 4 NW
1 1 SW
9 1 SE
8 3 NE
9 4 NE
7 4 NE
10 5 6 4
1 4 NW
1 1 SW
9 1 SE
8 3 NE
9 4 NE
7 4 NE
Sample OutputCase 1:
0 0
0 5
8 5
9 4
10 0
10 5
Case 2:
3 2
3 3
7 2
7 3
8 3
9 2
题意:给你一些球 可忽略半径,在一个矩形长为L 宽为 W 内直线运动,相互碰到或者碰到边都会以原速度大小 相反方向返回(这样其实也可以看成是没有返回继续运动,因为球都是一样的,速度也都一样,所以只用考虑与边界相撞),给你这些球的初始坐标及运动方向 东南 西北 东西 南北 等等, 问在 k秒后的它的坐标是?
题解:这道题如果出成数学题就非常简单,就是画个图,计算一下长度就行,建议自己画画图就很清晰了;
把小球分为x和y方向的运动,这样问题就变得很容易了;我们先假设小球向正方向运动:: (以矩形的左下角为坐标原点)
向右:我们发现小球K秒后运动的长度为2L,4L....小球依然在原点x所以我们可以先让k对2L取余,记为ans,再来讨论小球的位置:
1: 如果小球向右运动的距离ans小于L-x,那么此时小球的坐标为x+ans;(不会撞到边界)
2: 如果 ans>L-x的话,那么说明小球会从右边反弹,此时小球的坐标为L-(ans-(L-x)=2L-ans-x;(撞一次后运动方向到左边)
3: 如果ans>2L-x的话,说明小球会从左边反弹,此时小球的坐标为ans-(2L-x)=ans-2L+x;(撞两次后向右运动)
再假设小球向负方向运动:(和正方向时一样)
1: 如果小球想左运动的距离ams小于m的话,此时小球的坐标为m-ams;
2: 如果小球向左运动的距离ams大于m的话,小球的坐标为ams-m;
3: 如果ams>L+m的话,小球从右边反弹回来了,此时小球的坐标为L-(ans-(L+m))=2L-ans+m;
然后分东南西北即可 L方向和W方向一样;#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
}p[1010];
bool cmp(node a,node b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int up(int m,int l,int k)
{
int ans=k%(l*2);
if(ans>(l-m))
{
if(ans>=2*l-m)
m=ans+m-2*l;
else
m=2*l-ans-m;
}
else
m+=ans;
return m;
}
int down(int m,int l,int k)
{
int ans=k%(2*l);
if(ans>m)
{
if(ans>l+m)
m=2*l-ans+m;
else
m=ans-m;
}
else m-=ans;
return m;
}
int main()
{
int n,l,w,k,t,r=0;
char a,b;
cin>>t;
while(t--)
{
memset(p,0,sizeof(p));
scanf("%d %d %d %d",&l,&w,&n,&k);
for(int i=0;i<n;i++)
{
scanf("%d %d %c%c",&p[i].x,&p[i].y,&a,&b);
if(a=='N')
p[i].y=up(p[i].y,w,k);
else
p[i].y=down(p[i].y,w,k);
if(b=='E')
p[i].x=up(p[i].x,l,k);
else
p[i].x=down(p[i].x,l,k);
}
sort(p,p+n,cmp);
printf("Case %d:\n",++r);
for(int i=0;i<n;i++)
printf("%d %d\n",p[i].x,p[i].y);
}
return 0;
}
You are given the initial positions of the balls and their directions; your task is to find the position of the balls after K seconds. The board is placed in the 2D plane such that the boundaries are (0, 0), (L, 0), (L, W) and (0, W). The positions of balls are given as 2D co-ordinates and they all lie inside the board. And the directions are given as one of the following {NE, SE, SW, NW}, N, E, S and W denote North, East, South and West respectively. NE means North-East so both x and y are increasing. The balls are so small that their radiuses can be said to be 0. In each second, the balls advance one unit in their direction. Here one unit doesn't mean Euclidean one unit. For example, if the current position of a ball is (x, y) and its direction is NW then in the next second its position will be (x-1, y+1).
When two or more balls bounce off, they may change their directions as shown in the pictures. You can rotate the pictures to get all possible results. Remember that the balls may bounce at non-integer points.
Bouncing on walls
Bounce result between two balls
Bounce result amongst 3 balls and 4 balls
InputInput starts with an integer T (≤ 25), denoting the number of test cases.
Each case starts with a line containing four integers L, W (5 ≤ W ≤ L ≤ 108), n (1 ≤ n ≤ 1000) and K (1 ≤ K ≤ 108) where n denotes the number of balls in the board. Each of the next n lines contains two integers x y and a string d, where (x, y) (0 ≤ x ≤ L, 0 ≤ y ≤ W) denotes the co-ordinate of the ball and d can be one of {NE, SE, SW, NW} which denotes the direction of the ball respectively. You can safely assume that the balls are placed in different positions initially.
OutputFor each case, print the case number in a single line. Then print the position of the balls. Sort the positions first by their x co-ordinate, and order the ones which have same x coordinate by their y coordinates all in ascending order.
Sample Input2
10 5 6 1
1 4 NW
1 1 SW
9 1 SE
8 3 NE
9 4 NE
7 4 NE
10 5 6 4
1 4 NW
1 1 SW
9 1 SE
8 3 NE
9 4 NE
7 4 NE
Sample OutputCase 1:
0 0
0 5
8 5
9 4
10 0
10 5
Case 2:
3 2
3 3
7 2
7 3
8 3
9 2
题意:给你一些球 可忽略半径,在一个矩形长为L 宽为 W 内直线运动,相互碰到或者碰到边都会以原速度大小 相反方向返回(这样其实也可以看成是没有返回继续运动,因为球都是一样的,速度也都一样,所以只用考虑与边界相撞),给你这些球的初始坐标及运动方向 东南 西北 东西 南北 等等, 问在 k秒后的它的坐标是?
题解:这道题如果出成数学题就非常简单,就是画个图,计算一下长度就行,建议自己画画图就很清晰了;
把小球分为x和y方向的运动,这样问题就变得很容易了;我们先假设小球向正方向运动:: (以矩形的左下角为坐标原点)
向右:我们发现小球K秒后运动的长度为2L,4L....小球依然在原点x所以我们可以先让k对2L取余,记为ans,再来讨论小球的位置:
1: 如果小球向右运动的距离ans小于L-x,那么此时小球的坐标为x+ans;(不会撞到边界)
2: 如果 ans>L-x的话,那么说明小球会从右边反弹,此时小球的坐标为L-(ans-(L-x)=2L-ans-x;(撞一次后运动方向到左边)
3: 如果ans>2L-x的话,说明小球会从左边反弹,此时小球的坐标为ans-(2L-x)=ans-2L+x;(撞两次后向右运动)
再假设小球向负方向运动:(和正方向时一样)
1: 如果小球想左运动的距离ams小于m的话,此时小球的坐标为m-ams;
2: 如果小球向左运动的距离ams大于m的话,小球的坐标为ams-m;
3: 如果ams>L+m的话,小球从右边反弹回来了,此时小球的坐标为L-(ans-(L+m))=2L-ans+m;
然后分东南西北即可 L方向和W方向一样;#include<bits/stdc++.h>
using namespace std;
struct node
{
int x,y;
}p[1010];
bool cmp(node a,node b)
{
if(a.x==b.x)
return a.y<b.y;
return a.x<b.x;
}
int up(int m,int l,int k)
{
int ans=k%(l*2);
if(ans>(l-m))
{
if(ans>=2*l-m)
m=ans+m-2*l;
else
m=2*l-ans-m;
}
else
m+=ans;
return m;
}
int down(int m,int l,int k)
{
int ans=k%(2*l);
if(ans>m)
{
if(ans>l+m)
m=2*l-ans+m;
else
m=ans-m;
}
else m-=ans;
return m;
}
int main()
{
int n,l,w,k,t,r=0;
char a,b;
cin>>t;
while(t--)
{
memset(p,0,sizeof(p));
scanf("%d %d %d %d",&l,&w,&n,&k);
for(int i=0;i<n;i++)
{
scanf("%d %d %c%c",&p[i].x,&p[i].y,&a,&b);
if(a=='N')
p[i].y=up(p[i].y,w,k);
else
p[i].y=down(p[i].y,w,k);
if(b=='E')
p[i].x=up(p[i].x,l,k);
else
p[i].x=down(p[i].x,l,k);
}
sort(p,p+n,cmp);
printf("Case %d:\n",++r);
for(int i=0;i<n;i++)
printf("%d %d\n",p[i].x,p[i].y);
}
return 0;
}
相关文章推荐
- lightoj Basic Math 数论基础题 1414+1010+1020+1078+1116+1148+1179+1214+1275+1294+1297+1311+1323+1349+1354
- M - Billiard Balls LightOJ - 1323
- LightOJ 1291 Real Life Traffic
- LightOJ 1428 Melody Comparison
- LightOJ1248---Dice (III)(概率dp)
- lightOJ 1341 Aladdin and the Flying Carpet
- LightOJ 1282
- lightOj 1132(矩阵快速幂)
- LightOJ1008---Fibsieve`s Fantabulous Birthday (规律)
- LightOJ1011---Marriage Ceremonies (状压dp)
- lightOj 1079(期望)
- lightoj - 1205 Palindromic Numbers (数位dp)好
- LightOJ 1037
- LightOJ 1040
- Lightoj 1235 - Coin Change (IV) 【二分】
- LightOJ 1236 - Pairs Forming LCM (LCM·唯一分解)
- 周赛C题 LightOJ 1047 (DP)
- Lightoj 1094 - Farthest Nodes in a Tree 【树的直径裸题】
- lightoj 1036 - A Refining Company (dp)
- ACM第六周竞赛题目——A LightOJ 1317