您的位置:首页 > 其它

Uva 10881蚂蚁

2014-07-26 23:48 225 查看
#include<iostream>
#include<algorithm>
using namespace std;
int position;  //记录相撞时候的位置
const int maxn = 10000 + 5;
struct Ant
{
int id;
int p;
int d;
bool operator <(const Ant& a) const
{
return p<a.p;
}
} before[maxn],after[maxn];
const char dirName[][10] = {"L","Turning","R"};
int order[maxn];
int main()
{
int K;
cin>>K;
for(int i=1; i<=K; i++)
{
int L,T,n;
cout<<"case"<<i<<":"<<endl;
cin>>L>>T>>n;
for(int i=0; i<n; i++)
{
int p,d;
char c;
cin>>p>>c;
d = (c == 'L'? -1:1);//d为direction,即方向,左为-1,右为1;

//下面为录入每只蚂蚁的信息
before[i].id= i;
before[i].p = p;
before[i].d = d;

//经过移动后每只蚂蚁的信息
after[i].id = 0;
after[i].p = p+T*d;
after[i].d = d;
}

//蚂蚁的相对顺序是不变的,所以根据相对左端位置排序后,弄成编号.
sort(before,before+n);
for(int i=0; i<n; i++)
order[before[i].id] = i;
sort(after,after+n);
for(int i=0; i<n-1; i++)
{
if(after[i].p == after[i+1].p) //相撞的情况
{
position = after[i].p;   //保存相撞的位置,以便于输出
after[i].p = after[i+1].p = 0;//相撞置0
}
}
for(int i=0; i<n; i++)
{
int a = order[i]; //存入a可以便于下面代码的可读性.
if(after[a].p < 0 || after[a].p >L) cout<<"Fell off"<<endl;
else if(after[a].p == 0) cout<<position<<" "<<"Turning"<<endl;
else cout<<after[a].p<<" "<<dirName[after[a].d+1]<<endl;
}
cout<<endl;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: