题意:在一个长为L的木棍上有n只蚂蚁,每只蚂蚁要么往左爬,要么往右爬,如果两只蚂蚁相遇的话,二者同时掉头,计算经过t秒后所有蚂蚁的位置。
思路:这个题目看见这个题的时候,想模拟整个蚂蚁的爬行的过程,后来发现很难实现,就想到如果两只蚂蚁相遇以后,一只蚂蚁本来要到达的位置就是于它相遇的蚂蚁会到达的位置,这样一来就可以把终止状态下的蚂蚁的位置都模拟出来了(提前排一下序)。但是题目要求的是按输入顺序输出,那么我们就要在最终的状态下找到蚂蚁的顺序,一开始没有什么思路,后来画了一下图发现,所有蚂蚁在木棍上的相对位置是不会发生改变的,那这样标记一下就可以得到最后的顺序了。
//
// main.cpp
//
// Created by 尹贯宇 on 2017/6/29.
// Copyright © 2017年 尹贯宇. All rights reserved.
//
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#include <cmath>
#define LL long long
#define MAXN 10010
#define INF 1000000
#define MOD 1000000007
using namespace std;
struct Node {
int pos;
string dir;
string status;
int id;
};
bool cmp(const Node &x1, const Node &x2) {
return x1.pos < x2.pos;
}
bool cmp1(const Node &x1, const Node &x2) {
return x1.id < x2.id;
}
int main() {
ios::sync_with_stdio(false);
int T;
cin >> T;
Node ant[MAXN];
Node after[MAXN];
for (int kase = 1; kase <= T; ++kase) {
int l, t, n;
cin >> l >> t >> n;
for (int i = 0; i < n; ++i) {
cin >> ant[i].pos >> ant[i].dir;
ant[i].id = i + 1;
if (ant[i].dir == "L") {
after[i].pos = ant[i].pos - t;
after[i].dir = "L";
}
else {
after[i].pos = ant[i].pos + t;
after[i].dir = "R";
}
}
sort(ant, ant + n, cmp);
sort(after, after + n, cmp);
for (int i = 0; i < n; ++i) {
after[i].id = ant[i].id;
if (after[i].pos < 0 || after[i].pos > l)
after[i].status = "Fell off";
else if (i > 0 && after[i].pos == after[i - 1].pos) {
after[i].status = after[i - 1].status = "Turning";
}
else
after[i].status = after[i].dir;
}
cout << "Case #" << kase << ":\n";
sort(after, after + n, cmp1);
for (int i = 0; i < n; ++i) {
if (after[i].status == "Fell off")
cout << after[i].status << endl;
else
cout << after[i].pos << " " << after[i].status << endl;
}
cout << endl;
}
return 0;
}