您的位置:首页 > 其它

uva 10881(贪心)

2015-01-28 13:12 393 查看
题意:一个长度为L的木棍上有n只蚂蚁,给出了每只蚂蚁的初始位置和朝向,然后蚂蚁开始行走,如果两只蚂蚁相撞,就立刻掉头,直到掉下木棍,问t秒后每只蚂蚁的位置和状态。

题解:因为每只蚂蚁会因为碰撞而掉头,所以相对位置是不变的,为了更容易考虑计算,那么相撞的蚂蚁可以看做是对穿而过,得到输出结果时只要按初始的相对位置再对应上不同的蚂蚁就可以了。

#include <stdio.h>
#include <algorithm>
using namespace std;
const int N = 10005;
int l, T, n, order
;
struct Ant {
int dir;
int pos;
int flag;
}sta
, aft
;
char s[3][10] = {"L", "Turning", "R"};

int cmp(Ant a, Ant b) {
return a.pos < b.pos;
}

int main() {
int t, cas = 1;
scanf("%d", &t);
while (t--) {
char c;
scanf("%d%d%d", &l, &T, &n);
for (int i = 0; i < n; i++) {
scanf("%d %c", &sta[i].pos, &c);
sta[i].flag = i;
sta[i].dir = (c == 'L' ? -1 : 1);
aft[i].pos = sta[i].pos + T * sta[i].dir;
aft[i].dir = sta[i].dir;
}
sort(sta, sta + n, cmp);
for (int i = 0; i < n; i++)
order[sta[i].flag] = i;
sort(aft, aft + n, cmp);
for (int i = 0; i < n - 1; i++)
if (aft[i].pos == aft[i + 1].pos)
aft[i].dir = aft[i + 1].dir = 0;
printf("Case #%d:\n", cas++);
for (int i = 0; i < n; i++) {
if (aft[order[i]].pos < 0 || aft[order[i]].pos > l)
printf("Fell off\n");
else
printf("%d %s\n", aft[order[i]].pos, s[aft[order[i]].dir + 1]);
}
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva