uva 10881(贪心)

来源:互联网 发布:php项目架构设计文档 编辑:程序博客网 时间:2024/06/03 17:20

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

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

#include <stdio.h>#include <algorithm>using namespace std;const int N = 10005;int l, T, n, order[N];struct Ant {int dir;int pos;int flag;}sta[N], aft[N];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");elseprintf("%d %s\n", aft[order[i]].pos, s[aft[order[i]].dir + 1]);}printf("\n");}return 0;}


0 0
原创粉丝点击