刘汝佳《算法竞赛入门经典训练指南》1.1例5
///2014.4.13
///10881
///刘汝佳《算法竞赛入门经典训练指南》1.1例5
#include <iostream>
#include <cstdio>
#include <string>
#include <cmath>
#include <algorithm>
#include <climits>
using namespace std;
struct Ant{
int loc; ///location of the ant
int dir; ///direction , left:-1 , right:1
int id; ///the number of cin order
};
int L,T,n;
Ant ant[10100];
int loc[10100];
bool cmp(Ant a,Ant b){
return a.loc<b.loc;
}
bool cmp2(Ant a,Ant b){
return a.id<b.id;
}
int main()
{
// freopen("in","r",stdin);
// freopen("out","w",stdout);
int t;
cin>>t;
int cas = 1;
while( t-- ){
cin>>L>>T>>n;
for(int i=0 ; i<n ; i++){
cin>>ant[i].loc;
char temp;
cin>>temp;
switch( temp ){
case 'L': ant[i].dir = -1; break;
case 'R': ant[i].dir = 1; break;
}
ant[i].id = i;
}
sort(ant,ant+n,cmp);
for(int i=0 ; i<n ; i++){
loc[i] = ant[i].id;
}
for(int i=0 ; i<n ; i++){
ant[i].loc += ant[i].dir * T;
}
sort(ant,ant+n,cmp);
for(int i=0 ; i<n ; i++){
if( (ant[i].loc==ant[i+1].loc) && i+1<n ){
ant[i].dir=ant[i+1].dir=0;
i++;
}
}
for(int i=0 ; i<n ; i++){
ant[i].id = loc[i];
}
sort(ant,ant+n,cmp2);
cout<<"Case #"<<cas++<<':'<<endl;
for(int i=0 ; i<n ; i++){
if( ant[i].loc<0 || ant[i].loc>L )
cout<<"Fell off"<<endl;
else{
cout<<ant[i].loc<<" ";
switch( ant[i].dir ){
case 1: cout<<"R"<<endl; break;
case 0: cout<<"Turning"<<endl; break;
case -1: cout<<"L"<<endl;break;
}
}
}
cout<<endl;
}
return 0;
}