UVA 10881 Piotr's Ants(等效變換 sort結構體排序)

賈樹丙發表於2013-07-18
Piotr's Ants
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

2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R

Sample Output
Case #1:
2 Turning
6 R
2 Turning
Fell off

Case #2:
3 L
6 R
10 R

題目大意:一根長度為L釐米的木棍上有n只螞蟻,每隻螞蟻要麼朝左爬要麼朝右爬,速度為1釐米/秒。當兩隻螞蟻相撞時,二者同時掉頭(掉頭時間忽略不計)。給出每隻螞蟻初始位置和朝向,計算T秒之後每隻螞蟻的位置。

分析:在遠處觀察螞蟻運動,由於黑點太小,所以當螞蟻碰撞掉頭時,看上去和兩個點“對穿而過”沒有任何區別。換句話說,如果把螞蟻看成沒有區別的小點,那麼只需獨立計算出每隻螞蟻在T時刻的位置即可。比如,3只螞蟻,螞蟻1=(3,R),螞蟻2=(3,L),螞蟻3=(4,L),則兩秒鐘之後,3只螞蟻分別為(3,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 }

 

 

相關文章