Piotr's Ants
蚂蚁在一条长为L的木棍上走. 有n只蚂蚁. 给出每只蚂蚁的位置和方向. 给一个时间T.求出在T时间,每只蚂蚁的状态.
样例输入:
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
样例输出:
Case #1:
2 Turning
6 R
2 Turning
Fell off
Case #2:
3 L
6 R
10 R
用一个before记录移动之前的位置. 以便输出的时候和输入对应
用after记录运动之后的位置,两只蚂蚁相遇之后相当于不理睬对方继续向前.
以蚂蚁的位置对before排序是为了记录移动之前的位置.
对after排序是为了
1.让相遇的蚂蚁理论上换了位置
2.判断前后挨着的蚂蚁是否在同一个位置,如果在那么两只蚂蚁当前状态为 Turning
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string.h>
using namespace std;
typedef long long ll;
const int MAXN = 10000 + 100;
struct Ants{
int position;
char dir;
int index;
}before[MAXN],after[MAXN];
int order[MAXN];
int cmp(const Ants&ant1, const Ants&ant2){
return ant1.position < ant2.position;
}
int main(){
int t;
char s[2];
int n, L, T;
int position;
scanf("%d", &t);
int id = 0;
while (t--){
id++;
scanf("%d%d%d", &L, &T, &n);
for (int i = 0; i < n; i++){
scanf("%d%s", &position, s);
after[i].index = before[i].index = i;
before[i].position = position;
after[i].position = position + T * (s[0] == 'L' ? -1 : 1);
after[i].dir = before[i].dir = s[0];
}
sort(before, before + n, cmp);
for (int i = 0; i < n; i++){
order[before[i].index] = i;
}
sort(after, after + n, cmp);
for (int i = 0; i < n - 1; i++){
if (after[i].position == after[i + 1].position){
after[i].dir = 'T';
after[i + 1].dir = 'T';
}
}
printf("Case #%d:\n", id);
for (int i = 0; i < n; i++){
int j = order[i];
if (after[j].position < 0 || after[j].position > L) printf("Fell off\n");
else{
if(after[j].dir == 'T') printf("%d %s\n", after[j].position, "Turning");
else printf("%d %c\n", after[j].position, after[j].dir);
}
}
printf("\n");
}
#ifdef __wh
system("pause");
#endif
return 0;
}