知识点:思维,模拟
这个题有个操作和刚刚练习的链表一样,就是排完序之后,记录输入元素在有序序列里面的位置,这个题用到的知识主要就是问题转换的思维,移动之后,每个蚂蚁的绝对位置会改变,但是它的相对位置不会改变,这类题解法的基础就是来自这里,我们先输入元素,排序,记录一下输入的第几个元素在有序序列里面的位置,然后模拟蚂蚁走路,然后就要输出了,输出的时候我们遍历b数组,b[0]表示的就是第一个输入的元素在有序序列里面的位置,就是下标之间的映射,链表题里面就是地址之间的映射,然后我们分类讨论,输出就行了,这个题写了不到20分钟就过了
#include <bits/stdc++.h>
using namespace std;
const int N = 1e4 + 5;
struct node {
int x, r;
char dir;
} a[N];
bool cmp(node b, node c) {
return b.x < c.x;
}
int main() {
int T;
cin >> T;
int tt = 1;
while (T--) {
cout << "Case #" << tt++ << ":\n";
int l, t, n;
cin >> l >> t >> n;
for (int i = 0; i < n; i++) {
cin >> a[i].x >> a[i].dir;
a[i].r = i;
}
sort(a, a + n, cmp);
int b[N];
for (int i = 0; i < n; i++) {
b[a[i].r] = i;
if (a[i].dir == 'L') a[i].x -= t;
else a[i].x += t;
}
sort(a, a + n, cmp);
for (int i = 0; i < n; i++) {
int t = b[i];
if (a[t].x < 0 || a[t].x > l) {
cout << "Fell off\n";
} else {
cout << a[t].x << " ";
if (!t && a[t].x == a[t + 1].x) cout << "Turning\n";
else if (t == n - 1 && a[t].x == a[t - 1].x) cout << "Turning\n";
else if (t && t < n - 1 && (a[t].x == a[t - 1].x || a[t].x == a[t + 1].x)) cout << "Turning\n";
else cout << a[t].dir << endl;
}
}
cout << endl;
}
return 0;
}