UVA

来源:互联网 发布:左右声道测试软件 编辑:程序博客网 时间:2024/06/07 03:46

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, N. N 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 T seconds, print ‘Fell off’ for that ant. Print an empty line after each test case.
Sample Input
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Sample Output
Case #1:
2 Turning
6 R
2 Turning
Fell off
Case #2:
3 L
6 R
10 R

题意:
一根长L厘米的木棍上有n只蚂蚁,每只蚂蚁有个开始的位置和爬行方向,速度为1.当两只蚂蚁相撞后,两者同时掉头继续爬行,求按输入顺序给出每只蚂蚁T秒后的位置后朝向。

分析:
模拟蚂蚁行走,把位置排序,发现移动前后所有蚂蚁之间的相对位置不变,改变的只是方向。两只蚂蚁相撞后调头,可以看做对穿相过,毕竟蚂蚁之间没有区别。就当做这两只继续前进。
代码如下:

#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int maxn=10000+5;int idnum[maxn];//记录移动之前排序之后蚂蚁的编号//定义蚂蚁结构体class ant{public:    int id;//编号    int pos;//蚂蚁位置    int dir;//方向    ant(int a=0,int b=0,int c=0):id(a),pos(b),dir(c){}    bool operator<(const ant &p)const//重载<符号,方便sort函数的调用    {        return pos<p.pos;    }};char direction[10][10]={"L","Turning","R"};//方向数组int main(){    int T;    cin>>T;    int kase=0;    while(T--)    {        int l,t,n;        ant before[maxn],after[maxn];       cin>>l>>t>>n;        for(int i=0;i<n;i++)        {            int po;            char ch;            cin>>po;           getchar();            cin>>ch;             int tmp=(ch=='R'?1:-1);//为方向赋值,与方向数组配合使用             before[i]=ant(i,po,tmp);//记录移动前数据             after[i]=ant(0,po+tmp*t,tmp);//记录移动后数据        }        sort(before,before+n);//从小到大对蚂蚁按照位置排序        for(int i=0;i<n;i++)//移动前后相对位置不变化           idnum[before[i].id]=i;        sort(after,after+n);        for(int i=0;i<n-1;i++)//如果位置相同,改变方向为“Turning”        {            if(after[i].pos==after[i+1].pos)                after[i].dir=after[i+1].dir=0;        }        printf("Case #%d:\n",++kase);        for(int i=0;i<n;i++)        {            int tmp=idnum[i];            if(after[tmp].pos<0||after[tmp].pos>l)//越界                printf("Fell off\n");            else                cout<<after[tmp].pos<<" "<<direction[after[tmp].dir+1]<<endl;        }        printf("\n");    }    }
0 0
原创粉丝点击