Piotr's Ants

Description

Piotr likes playing with ants. He has n of them on a horizontal pole L cm long. Each ant is facing either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it is facing and wants to calculate where the ants will end up T seconds from now.

Input

The first line of input gives the number of cases, NN test cases follow. Each one starts with a line containing 3 integers: L , T and n (0 <= n <= 10000). The next n lines give the locations of the n ants (measured in cm from the left end of the pole) and the direction they are facing (L or R).

Output
For each test case, output one line containing "Case #x:" followed by n lines describing the locations and directions of the n ants in the same format and order as in the input. If two or more ants are at the same location, print "Turning" instead of "L" or "R" for their direction. If an ant falls off the pole before Tseconds, print "Fell off" for that ant. Print an empty line after each test case.

Sample Input Sample Output
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Case #1:
2 Turning
6 R
2 Turning
Fell off

Case #2:
3 L
6 R
10 R

拿到这个题,我整理出来了一些思路,但是都不能有效的解决这个问题。后来看了作者的解题思路,才恍然大悟。我们把蚂蚁看成一样的,都是一群黑点在移动,那么相互碰撞之后调转方向就可以看作是穿过对方一直向前。这样很容易可以得出最终蚂蚁们在木棍上的位置。但是,蚂蚁毕竟是不同的,这些位置都对应那些蚂蚁呢?我们发现,蚂蚁相互碰撞就调转方向这一特性,使得蚂蚁的相对位置不会改变,也就是说假设最开始在木棍上(从左到右)蚂蚁的顺序为:蚂蚁2、1、3,则最终还是2、1、3。因为输入时并非按照蚂蚁在木棍上的位置(从左到右)输入,所以我们需要在输入之后把蚂蚁的位置排序,并记录下蚂蚁的序号顺序 如2、1、3,位置变换后再把新位置排一次顺序,并将最开始的蚂蚁顺序2、1、3赋给新位置,最后再按照输入的顺序1、2、3将蚂蚁位置输出即可。代码如下:

#include
#include
#include
#include
int order[10001]; 
using namespace std;
struct Nobe
{
	int x;
	int s;
	string f; 
}S[10001];
int cmps(void const*a,void const*b)
{
	return(*(Nobe*)a).s-(*(Nobe*)b).s;
}
int cmpx(void const*a,void const*b)
{
	return(*(Nobe*)a).x-(*(Nobe*)b).x;
}
int main()
{
	int group,count=0;
	cin>>group;
	while(group--)
	{
		int L,T,n;
		cin>>L>>T>>n;
		for(int i=0;i>S[i].s>>S[i].f;
			S[i].x=i+1;
		}
		qsort(S,n,sizeof(S[0]),cmps);   //最初的位置排序
		for(int i=0;iL) printf("Fell off\n");
			else  cout<


你可能感兴趣的:(Piotr's Ants)