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

一共有N个测试实例。每个实例输入L,T,n代表木棒的长度,规定的T秒,n个蚂蚁。接下来n行每行一个x代表此蚂蚁的坐标和一个字母表示初始朝向。Turning表示正在碰撞,Fell off表示已经掉下木棍。

这道题跟《蚂蚁感冒》那道题做法差不多,当蚂蚁因为碰撞而掉头时实际上我们看到的效果跟对穿而过没有任何差别。但是对于每只蚂蚁来说却不是这样的,我们只知道有一只蚂蚁在那个位置但是却不知道是哪一只。但是所有蚂蚁的相对顺序是不变的(为什么呢,因为两只蚂蚁碰撞后会返回也就是说1号蚂蚁一定在2号蚂蚁左边),因此把所有目标位置(T秒之后各个蚂蚁的位置)从小到大排序,则从左到右的每个位置对应于初始状态下从左到右的每只蚂蚁,需要预处理计算出输入中的第i只蚂蚁的序号


#include <iostream>
#include <algorithm>
using namespace std;
struct node
{
    int start; //开始与结尾位置
    int der;//-1表示左,0表示碰撞,1表示右
    int id; //输入顺序
};
bool cmp(node a,node b)
{
    return a.start<=b.start;//对蚂蚁的位置排序
}
string dername[3]={"L","Turning","R"};
int main()
{
    int T;
    cin>>T;
    for(int M=1;M<=T;M++)
    {
        node before[10000],after[10000];
        long long order[10000];
        int L,T,n;
        cin>>L>>T>>n;
        for(int i=0;i<n;i++)
        {
            int pos,d;
            char c;
            cin>>pos>>c;
            d=(c=='L'?-1:1);
            before[i].start=pos;
            before[i].der=d;
            before[i].id=i;
            after[i].start=pos+T*d;
            after[i].der=d;
            after[i].id=0;//方向不知所以赋为0
        }
        sort(before,before+n,cmp);//输入顺序不一定按照从左往右的顺序输,所以将他们排好序
        for(int i=0;i<n;i++)
            order[before[i].id]=i;//这个是输出的时候用,
        sort(after,after+n,cmp);
        for(int i=0;i<n-1;i++)
           if(after[i].start==after[i+1].start)//修改碰撞中蚂蚁的方向
            after[i].der=after[i+1].der=0;
        cout<<"Case #"<<M<<":"<<endl;
        for(int i=0;i<n;i++)
        {
            int a=order[i];
            if(after[a].start<0||after[a].start>L)cout<<"Fell off"<<endl;
            else cout<<after[a].start<<" "<<dername[after[a].der+1]<<endl;
        }
        cout<<endl;
    }
    return 0;
}


posted on 2015-04-17 17:27  星斗万千  阅读(107)  评论(0编辑  收藏  举报