/*
这题是新白书上的例题 看了上面的做法 我想了好久才想明白。
首先在距离的概念上 2个蚂蚁相遇 掉头 等于 对穿而过。所以在距离上可以直接计算终态
但每只蚂蚁位置的ID会变化,所以在这个问题上我纠结了很长时间才明白
第i个蚂蚁无论怎么走还是在第i个位置上 如:第i个蚂蚁往右走 若遇到第i+1个 会掉头,还是在第i个位置
同理往左走也一样, 所以只需要记录蚂蚁的初始位置就可以。
*/
#include<cstdio>
#include<cstring>#include<cstdlib>
int l,k,n;
struct ant
{
int id,f,d;// 方向 距离
} a[10010],b[10010];
char dir[4][14] = {"Turning","L","R"};
int path[10010];
int cmp(const void *a,const void *b)
{
struct ant *p = (struct ant *)a;
struct ant *q = (struct ant *)b;
return p->d-q->d;
}
int main()
{
int t,count=0;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d",&l,&k,&n);
char tf;
int d1;
for(int i = 0; i < n; i++)
{
scanf("%d %c",&d1,&tf);
a[i].id = i;
a[i].f = (tf=='L'?1:2);
a[i].d = d1;
int p = (tf=='L'?-1:1);
b[i].d = d1+k*p;
b[i].f = a[i].f;
}
qsort(a,n,sizeof(a[0]),cmp);
for(int i = 0; i < n; i++)
path[a[i].id]=i;
qsort(b,n,sizeof(b[0]),cmp);
for(int i = 0; i < n-1; i++)
if(b[i].d ==b[i+1].d) b[i].f=b[i+1].f=0;
printf("Case #%d:\n",++count);
for(int i = 0; i < n; i++)
{
int tm = path[i];
if(b[tm].d > l|| b[tm].d<0)
printf("Fell off\n");
else printf("%d %s\n",b[tm].d,dir[b[tm].f]);
}
printf("\n");
}
return 0;
}