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 InputSample 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<iostream>
#include<string>
#include<stdlib.h>
#include<cstdio>
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<n;i++)
		{
			cin>>S[i].s>>S[i].f;
			S[i].x=i+1;
		}
		qsort(S,n,sizeof(S[0]),cmps);   //最初的位置排序
		for(int i=0;i<n;i++) order[i]=S[i].x;  //记录蚂蚁排列顺序
		for(int i=0;i<n;i++)
		{
			if(S[i].f=="R")
				S[i].s=S[i].s+T;
			if(S[i].f=="L")
				S[i].s=S[i].s-T;
		}
		qsort(S,n,sizeof(S[0]),cmps);  //新位置排序
		for(int i=0;i<n-1;i++)
		{
			if(S[i].s==S[i+1].s)
			{
				S[i].f="Turning";
				S[i+1].f="Turning";
			}
		}
		for(int i=0;i<n;i++) S[i].x=order[i];  //把蚂蚁最初的排列顺序赋给新位置
		qsort(S,n,sizeof(S[0]),cmpx);
		printf("Case #%d:\n",++count);
		for(int i=0;i<n;i++)
		{
			if(S[i].s<0||S[i].s>L) printf("Fell off\n");
			else  cout<<S[i].s<<" "<<S[i].f<<endl;
		}
		cout<<endl; //勿忘!
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值