您的位置:首页 > 其它

【UVA 10881】 经典模拟题

2012-11-12 16:50 337 查看
题目链接:http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=25979

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

解题思路:

1.每只蚂蚁相撞后同时掉头可以看做对穿而过,关键的问题就在于求位置的变化。

2.按位置从小到大排序,可以惊奇的发现排序后(befor数组和after数组)所有的蚂蚁相对位置并没有变化,改变的只是朝向。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn=10005;

struct node
{
int   id, p, d;   //d表示朝向,-1表示左,0表示碰撞中,1表示右。
bool operator < (const node& S)const
{
return p<S.p;
}
} befor[maxn],after[maxn];

char dirname[][10]={"L","Turning","R"};
int  order[maxn];

int main()
{
int  T, n, L, t, tcase=0;
int  ss[10005];
cin >> T;
while(T--)
{
scanf("%d%d%d",&L, &t, &n);
printf("Case #%d:\n",++tcase);
for(int i=0; i<n; i++)
{
int  p, d;
char c;
scanf("%d %c",&p,&c);
d=(c=='L'?-1:1);
befor[i]=(node){i,p,d};
after[i]=(node){0,p+t*d,d};  //所以的蚂蚁相撞后可以看做对穿而过
}
sort(befor,befor+n);
for(int i=0; i<n; i++)  //最巧妙的地方在这里,第一次从左到右所有的蚂蚁的相对位置没有变化
order[befor[i].id]=i;
sort(after,after+n);
for(int i=0; i<n-1; i++)
if(after[i].p==after[i+1].p)  after[i].d=after[i+1].d=0;
for(int i=0; i<n; i++)
{
int a=order[i];
if(after[a].p>=0&&after[a].p<=L)
{
printf("%d %s\n",after[a].p,dirname[after[a].d+1]);
}
else
printf("Fell off\n");
}
puts("");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: