UVa 10881 Piotr’s Ants

Piotr's Ants
Time Limit: 2 seconds
"One thing is for certain: there is no stopping them;
the ants will soon be here. And I, for one, welcome our
new insect overlords."

Kent Brockman

Piotr likes playing with ants. He has n of them on a horizontal poleL 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 upT seconds from now.

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 nextn lines give the locations of then 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 byn lines describing the locations and directions of then 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 polebefore T seconds, print "Fell off" for that ant. Print an empty line after each test case.

Sample Input Sample Output
2
10 1 4
1 R
5 R
3 L
10 R
10 2 3
4 R
5 L
8 R
Case #1:
2 Turning
6 R
2 Turning
Fell off

Case #2:
3 L
6 R
10 R

 蚂蚁相对位置不会改变

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <map>
 4 #include <algorithm>
 5 using namespace std;
 6 map <int,int> v;
 7 struct Node
 8 {
 9     int loc;
10     char direc;
11     int num;
12 }a[10005];
13 bool cmp(Node x,Node y)
14 {
15     return x.loc<y.loc;
16 }
17 bool cmp2(Node x,Node y)
18 {
19     return x.num<y.num;
20 }
21 int main()
22 {
23     int T,n,i,j,k=0,l,t;
24     int nu[10005];
25     scanf("%d",&T);
26     while(T--)
27     {
28         k++;
29         v.clear();
30         scanf("%d%d%d",&l,&t,&n);
31         for(i=1;i<=n;i++)
32         {
33             scanf("%d",&a[i].loc);
34             getchar();
35             scanf("%c",&a[i].direc);
36             a[i].num=i;
37         }
38         sort(a+1,a+n+1,cmp);
39         for(i=1;i<=n;i++)
40             nu[i]=a[i].num;
41         for(i=1;i<=n;i++)
42         {
43             if(a[i].direc=='L')
44             {
45                 a[i].loc=a[i].loc-t;
46             }
47             else if(a[i].direc=='R')
48             {
49                 a[i].loc=a[i].loc+t;
50             }
51             v[a[i].loc]++;
52         }
53         sort(a+1,a+n+1,cmp);
54         for(i=1;i<=n;i++)
55             a[i].num=nu[i];
56         /*for(i=1;i<=n;i++)
57             printf("%d ",a[i].num);
58         printf("&&*
");*/
59         sort(a+1,a+n+1,cmp2);
60         printf("Case #%d:
",k);
61         for(i=1;i<=n;i++)
62         {
63             if(a[i].loc<0 || a[i].loc>l)
64                 printf("Fell off
");
65             else
66             {
67                 if(v[a[i].loc]>1)
68                     printf("%d Turning
",a[i].loc);
69                 else
70                     printf("%d %c
",a[i].loc,a[i].direc);
71             }
72         }
73         printf("
");
74         /*for(i=1;i<=n;i++)
75             printf("%d ",a[i].num);
76         printf("&&*
");*/
77     }
78     return 0;
79 }
View Code
原文地址:https://www.cnblogs.com/cyd308/p/4669812.html