Piotr's Ants

来源:互联网 发布:双人無心内事无人知 编辑:程序博客网 时间:2024/05/01 09:10

转自:http://www.cnblogs.com/acm-bingzi/archive/2013/07/18/3198338.html


Time Limit: 2 seconds

 


Piotr likes playing with ants. He has 
n of them on a horizontal pole L cm long. Each ant is facing either left or right and walks at a constant speed of 1 cm/s. When two ants bump into each other, they both turn around (instantaneously) and start walking in opposite directions. Piotr knows where each of the ants starts and which direction it is facing and wants to calculate where the ants will end up T seconds from now.Kent Brockman

Input
The first line of input gives the number of cases, NN test cases follow. Each one starts with a line containing 3 integers: L , T and n (0 <= n <= 10000). The next n lines give the locations of the n ants (measured in cm from the left end of the pole) and the direction they are facing (L or R).

Output
For each test case, output one line containing "Case #x:" followed by n lines describing the locations and directions of the n ants in the same format and order as in the input. If two or more ants are at the same location, print "Turning" instead of "L" or "R" for their direction. If an ant falls off the pole before Tseconds, print "Fell off" for that ant. Print an empty line after each test case.

Sample Input

210 1 41 R5 R3 L10 R10 2 34 R5 L8 RSample Output
Case #1:2 Turning6 R2 TurningFell offCase #2:3 L6 R10 R题目大意:一根长度为L厘米的木棍上有n只蚂蚁,每只蚂蚁要么朝左爬要么朝右爬,速度为1厘米/秒。当两只蚂蚁相撞时,二者同时掉头(掉头时间忽略不计)。给出每只蚂蚁初始位置和朝向,计算T秒之后每只蚂蚁的位置。分析:在远处观察蚂蚁运动,由于黑点太小,所以当蚂蚁碰撞掉头时,看上去和两个点“对穿而过”没有任何区别。换句话说,如果把蚂蚁看成没有区别的小点,那么只需独立计算出每只蚂蚁在T时刻的位置即可。比如,3只蚂蚁,蚂蚁1=(3,R),蚂蚁2=(3,L),蚂蚁3=(4,L),则两秒钟之后,3只蚂蚁分别为(5,R),(1,L)和(2,L)。而且所有蚂蚁的相对顺序保持不变,因此把所有目标位置从小到大排序,则从左到右的每个位置对应于初始状态下从左到右的每只蚂蚁。由于原题中的蚂蚁不一定按照从左到右的顺序输入,还需要预处理计算出输入中的第i只蚂蚁的序号order[i]。代码如下:
复制代码
 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4  5 const int maxn = 10000 + 5; 6  7 struct Ant { 8   int id; // 输入顺序 9   int p;  // 位置10   int d;  // 朝向。 -1: 左; 0:转身中; 1:右11   bool operator < (const Ant& a) const {12     return p < a.p;13   }14 } before[maxn], after[maxn];15 16 const char dirName[][10] = {"L", "Turning", "R"};17 18 int order[maxn]; // 输入的第i只蚂蚁是终态中的左数第order[i]只蚂蚁19 20 int main() {21   int K;22   scanf("%d", &K);23   for(int kase = 1; kase <= K; kase++) {24     int L, T, n;25     printf("Case #%d:\n", kase);26     scanf("%d%d%d", &L, &T, &n);27     for(int i = 0; i < n; i++) {28       int p, d;29       char c;30       scanf("%d %c", &p, &c);31       d = (c == 'L' ? -1 : 1);32       before[i] = (Ant){i, p, d};33       after[i] = (Ant){0, p+T*d, d}; // 这里的id是未知的34     }35 36     // 计算order数组37     sort(before, before+n);38     for(int i = 0; i < n; i++)39       order[before[i].id] = i;40 41     // 计算终态42     sort(after, after+n);    43     for(int i = 0; i < n-1; i++) // 修改碰撞中的蚂蚁的方向44       if(after[i].p == after[i+1].p) after[i].d = after[i+1].d = 0;45 46     // 输出结果47     for(int i = 0; i < n; i++) {48       int a = order[i]; 49       if(after[a].p < 0 || after[a].p > L) printf("Fell off\n");50       else printf("%d %s\n", after[a].p, dirName[after[a].d+1]);51     }52     printf("\n");53   }54   return 0;55 }
复制代码

=============

转自:http://www.cnblogs.com/fzf123/archive/2012/10/29/2745294.html

训练指南上的一道题目,感觉还不错。

参考该书做如下总结:

题意:一根长度为L的木棍上有n只蚂蚁,每只蚂蚁要么朝左要么朝右爬,求T秒后每只蚂蚁的位置和状态。

分析:1)蚂蚁相撞“掉头”等价于“对穿而过”;

     2)每只蚂蚁最后位置固定;

     3)初始时按位置排序后,第i只蚂蚁不可能比第i+1只蚂蚁的距离远。